c++11 有没有办法防止“正常”在原子操作之前或之后滑动的操作

发布于 2024-12-05 11:49:37 字数 976 浏览 1 评论 0原文

我有兴趣做类似的事情(单线程更新,多线程读取 banneedURLs):

atomic<bannedURLList*> bannedURLs;//global variable pointing to the currently used instance of struct
void  updateList()
{
       //no need for mutex because only 1 thread updates
       bannedURLList* newList= new bannedURLList();
       bannedURLList* oldList=bannedURLs;
       newList->initialize();
       bannedURLs=newList;// line must be after previous line, because list must be initialized before it is ready to be used

      //while refcnt on the oldList >0 wait, then delete oldList;
}

读取器线程做这样的事情:

{
   bannedURLs->refCnt++;
   //use bannedURLs
   bannedURLs->refCnt--;
}

struct memeber refCnt 也是原子整数 我的问题是如何防止这两行重新排序:

newList->initialize();
bannedURLs=newList;

可以以 std:: 方式完成吗?

I'm interested in doing something like(single thread update, multiple threads read banneedURLs):

atomic<bannedURLList*> bannedURLs;//global variable pointing to the currently used instance of struct
void  updateList()
{
       //no need for mutex because only 1 thread updates
       bannedURLList* newList= new bannedURLList();
       bannedURLList* oldList=bannedURLs;
       newList->initialize();
       bannedURLs=newList;// line must be after previous line, because list must be initialized before it is ready to be used

      //while refcnt on the oldList >0 wait, then delete oldList;
}

reader threads do something like this:

{
   bannedURLs->refCnt++;
   //use bannedURLs
   bannedURLs->refCnt--;
}

struct memeber refCnt is also atomic integer
My question is how to prevent reordering of this 2 lines:

newList->initialize();
bannedURLs=newList;

Can it be done in std:: way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-12-12 11:49:37

使用 bannedURLs.store(newList); 而不是 bannedURLs=newList;。由于您没有传递弱排序说明符,因此这会强制在商店中进行完全排序。

Use bannedURLs.store(newList); instead of bannedURLs=newList;. Since you didn't pass a weak ordering specifier, this forces full ordering in the store.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文