jjzjj

c++ - 我可以使以下代码无锁/原子锁吗?

intval=memLoc[index++];或者更好intval=memLoc[index++&0xFF];尝试从共享环形缓冲区中进行线程安全读取,每次调用都会在其中获取下一个值-我希望它尽可能无锁,因为它发生了TON。不允许使用Boost/C++11:( 最佳答案 此处唯一需要同步的操作是index值的递增。由于这只是一个数值,因此可以通过原子增量在不使用锁的情况下完成。您列出的其余操作只是共享位置的读取,不需要同步。在Win32上同步增量是通过InterlockedIncrement函数完成的intoldValue=Inter

c++ - 从多个线程更新最大值

有没有办法使用原子操作从多个线程更新最大值?示例:std::vectorcoord_max(128);#pragmaompparallelforfor(inti=0;i在上面的例子中,关键部分同步访问整个vector,而我们只需要独立同步访问每个值。 最佳答案 根据评论中的建议,我找到了一个不需要锁定的解决方案,而是使用std::atomic/boost::atomic中的比较和交换功能。我仅限于C++03,所以在这种情况下我会使用boost::atomic。BOOST_STATIC_ASSERT(sizeof(int)==size

C++ 成员在非原子时更新关键部分内的可见性

我偶然发现了thefollowingCodeReviewStackExchange并决定阅读它作为练习。在代码中,有以下内容:注意:我不是在寻找代码审查,这只是链接中代码的复制粘贴,因此您可以专注于手头的问题,而无需其他代码干扰。我对实现“智能指针”不感兴趣,只是了解内存模型://Copiedfromthelinkprovided(allinsideaclass)unsignedintcount;mutexm_Mutx;voidderef(){m_Mutx.lock();count--;m_Mutx.unlock();if(count==0){deleterawObj;count=0;

c++ - 命名空间中的全局变量 - 线程中的值不同

考虑以下情况:2个不同的网络端口,通过boost::asio每个都有自己的线程1个端口正在接收和处理数据-classDataConnection包裹在std::thread中1个端口用于发送统计信息classStatConnection也包裹在std::thread中为了计算连接数(和其他小数据片段),我的想法是使用staticnamespace中的变量喜欢:#includenamespaceapp{namespacestatus{staticstd::atomiccounter=0;}}这适用于DataConnection类(class)。这里我递增counter在c'tor中并查看

c++ - 使用 atomic_flag 自旋锁进行内存排序

我正在尝试熟悉c++11的新内存排序概念,并且相信我实际上已经很好地掌握了它们,直到我偶然发现了自旋锁的这个实现:#includenamespaceJayZ{namespaceTools{classSpinLock{private:std::atomic_flagspin_lock;public:inlineSpinLock(void):atomic_flag(ATOMIC_FLAG_INIT){}inlinevoidlock(void){while(spin_lock.test_and_set(std::memory_order_acquire));}inlinevoidunlock

C++11 原子 : why does this code work?

让我们采用这个结构:structentry{atomicvalid;atomic_flagwriting;charpayload[128];}两个线程A和B以这种方式同时访问这个结构(让e成为entry的一个实例):if(e.valid){//dosomethingwithe.payload...}else{while(e.writing.test_and_set(std::memory_order_acquire));if(!e.valid){//writee.payloadonebyteatatime//(thepayloadwrittenbyAmaybedifferentfrom

c++ - std::atomic<double> & 的模板特化

我有这个MCVE:#include#includetemplatevoidassertVariableHasBeenSet(T,constchar*);templatevoidassertVariableHasBeenSet&>(std::atomic&myDouble,constchar*variableName){printf("Double:%s=%f\n",variableName,myDouble.load());};intmain(){std::atomicmyDoubleAtomic{23.45};assertVariableHasBeenSet(myDoubleAtom

c++ - 是否可以在不修改 C++ 的情况下使用 C11 原子包含 C header ?

我正在尝试编写使用thisClibrary在C++应用程序中未经修改。它使用C11原子。考虑以下程序,我们可以将其放入名为main.cc的文件中。#include"mpscq.h"intmain(){}如果我用g++-std=c++11-cmain.cc编译它,我会得到一整套错误,如下所示。usr/lib/gcc/x86_64-linux-gnu/4.9/include/stdatomic.h:68:9:error:‘_Atomic’doesnotnameatypetypedef_Atomic__UINT_FAST32_TYPE__atomic_uint_fast32_t;^/usr/

c++ - is_lock_free 未在 gcc 4.7.2 的 std::atomic<T> 中定义?

我遇到这个编译器错误functionstd::atomic::is_lock_free()const:error:undefinedreferenceto'__atomic_is_lock_free'whencompilingcodelikebelowusinggcc4.7.2onlinux.structS{inta;intb;};std::atomics;cout 最佳答案 AtomicAPIisn'tcompleteinGCC4.7:Whenlockfreeinstructionsarenotavailable(eitherth

c++ - c++11(atomic)的获取释放操作

#include#include#includeclassatomicAcquireRelease00{public:atomicAcquireRelease00():x(false),y(false),z(0){}voidrun(){std::threada(&atomicAcquireRelease00::write_x,this);std::threadb(&atomicAcquireRelease00::write_y,this);std::threadc(&atomicAcquireRelease00::read_x_then_y,this);std::threadd(&at