草庐IT

原子钟

全部标签

c++ - C++11 中 shared_ptr 的原子操作

通过阅读c++11草案n3242,第20.7.2.5节,看起来我们对shared_ptr进行了原子操作,这使我们能够对复杂结构进行无锁操作,而无需担心GC/内存泄漏。但是,我无法在GCC-4.7.0中成功使用它。我只是测试了以下程序#include#include#includestructX{intx;doubley;std::strings;};intmain(){std::shared_ptrx(newX);autop=std::atomic_load(&x);}它有编译器错误:c.cpp:13:33:error:nomatchingfunctionforcallto‘atomi

c++ - C++11 中 shared_ptr 的原子操作

通过阅读c++11草案n3242,第20.7.2.5节,看起来我们对shared_ptr进行了原子操作,这使我们能够对复杂结构进行无锁操作,而无需担心GC/内存泄漏。但是,我无法在GCC-4.7.0中成功使用它。我只是测试了以下程序#include#include#includestructX{intx;doubley;std::strings;};intmain(){std::shared_ptrx(newX);autop=std::atomic_load(&x);}它有编译器错误:c.cpp:13:33:error:nomatchingfunctionforcallto‘atomi

c++ - 如果在互斥锁下没有修改共享原子变量,则无法正确发布

我正在阅读http://en.cppreference.com/w/cpp/thread/condition_variable上有关std::condition_variable的信息我不明白这一点:Evenifthesharedvariableisatomic,itmustbemodifiedunderthemutexinordertocorrectlypublishthemodificationtothewaitingthread.如果共享原子变量没有在互斥锁下修改,为什么不能正确发布?如何理解这句话?在另一页http://en.cppreference.com/w/cpp/ato

c++ - 如果在互斥锁下没有修改共享原子变量,则无法正确发布

我正在阅读http://en.cppreference.com/w/cpp/thread/condition_variable上有关std::condition_variable的信息我不明白这一点:Evenifthesharedvariableisatomic,itmustbemodifiedunderthemutexinordertocorrectlypublishthemodificationtothewaitingthread.如果共享原子变量没有在互斥锁下修改,为什么不能正确发布?如何理解这句话?在另一页http://en.cppreference.com/w/cpp/ato

c++ - 如何实现原子(线程安全)和异常安全的深拷贝赋值运算符?

面试的时候被问到这个问题,我也不好回答。更具体地说,赋值运算符所属的类如下所示:classA{private:B*pb;C*pc;....public:....}如何为此类实现原子(线程安全)和异常安全的深度复制赋值运算符? 最佳答案 有两个独立的关注点(线程安全和异常安全),最好分别解决它们。为了允许构造函数将另一个对象作为参数在初始化成员时获取锁,无论如何必须将数据成员分解为一个单独的类:这样可以在初始化子对象和维护实际数据的类时获取锁可以忽略任何并发问题。因此,该类将分为两部分:classA处理并发问题和classA_unlo

c++ - 如何实现原子(线程安全)和异常安全的深拷贝赋值运算符?

面试的时候被问到这个问题,我也不好回答。更具体地说,赋值运算符所属的类如下所示:classA{private:B*pb;C*pc;....public:....}如何为此类实现原子(线程安全)和异常安全的深度复制赋值运算符? 最佳答案 有两个独立的关注点(线程安全和异常安全),最好分别解决它们。为了允许构造函数将另一个对象作为参数在初始化成员时获取锁,无论如何必须将数据成员分解为一个单独的类:这样可以在初始化子对象和维护实际数据的类时获取锁可以忽略任何并发问题。因此,该类将分为两部分:classA处理并发问题和classA_unlo

C++11 (g++ thread sanitized) 用原子排序非原子操作(误报?)

我正在尝试使用g++和线程sanitizer,但我认为我得到了误报。这是真的吗,还是我犯了大错?程序(来自AnthonyWilliams:C++ConcurrencyinAction,第145页,list5.13)#include#include#includeboolx=false;std::atomicy;std::atomicz;voidwrite_x_then_y(){x=true;std::atomic_thread_fence(std::memory_order_release);y.store(true,std::memory_order_relaxed);}voidre

C++11 (g++ thread sanitized) 用原子排序非原子操作(误报?)

我正在尝试使用g++和线程sanitizer,但我认为我得到了误报。这是真的吗,还是我犯了大错?程序(来自AnthonyWilliams:C++ConcurrencyinAction,第145页,list5.13)#include#include#includeboolx=false;std::atomicy;std::atomicz;voidwrite_x_then_y(){x=true;std::atomic_thread_fence(std::memory_order_release);y.store(true,std::memory_order_relaxed);}voidre

c++ - 这个用于原子浮点的 C++ 实现安全吗?

Edit:Thecodeherestillhassomebugsinit,anditcoulddobetterintheperformancedepartment,butinsteadoftryingtofixthis,fortherecordItooktheproblemovertotheInteldiscussiongroupsandgotlotsofgreatfeedback,andifallgoeswellapolishedversionofAtomicfloatwillbeincludedinanearfuturereleaseofIntel'sThreadingBuildi

c++ - 这个用于原子浮点的 C++ 实现安全吗?

Edit:Thecodeherestillhassomebugsinit,anditcoulddobetterintheperformancedepartment,butinsteadoftryingtofixthis,fortherecordItooktheproblemovertotheInteldiscussiongroupsandgotlotsofgreatfeedback,andifallgoeswellapolishedversionofAtomicfloatwillbeincludedinanearfuturereleaseofIntel'sThreadingBuildi