草庐IT

atomic-values

全部标签

c++ - 两个 std::atomic 可以成为一个 union 的一部分吗?

我想这样做:union{std::atomicu128;struct{std::atomicu64_1;std::atomicu64_2;};};多个线程将读取和写入union的两个部分。安全吗?编辑:我使用Linux,x86_64,clang3.3Edit2:我希望能够递增和递减u64_1,读取u64_2,并写入u128(compare_exchange)Edit3:如果我使用atomicbuiltinfunctions会怎么样??union将如下所示:union{uint128_tu128;struct{uint64_tu64_1;uint64_tu64_2;};};u64_1将映

c++ - 如果我按值(value)捕获但不使用它,我还能得到一份拷贝吗?

我不想在lambda中按值捕获shared_ptr以确保对象的生命周期延长到调用lambda函数的时间点。否则我实际上不需要shared_ptr。如果我这样做:shared_ptrsp;sp->async_call([sp](){});sp是否保证被复制,即使正文没有引用它? 最佳答案 我会说这是有保证的,因为这句话。5.1.221Whenthelambda-expressionisevaluated,theentitiesthatarecapturedbycopyareusedtodirect-initializeeachcorr

c++ - '_ITERATOR_DEBUG_LEVEL' : value '0' doesn't match value '2' 检测到不匹配

使用VS2010构建时,我正在构建一个导致许多此类链接错误的库:errorLNK2038:mismatchdetectedfor'_ITERATOR_DEBUG_LEVEL':value'0'doesn'tmatchvalue'2'导致我必须同时发布我的库的发行版和调试版。我没有理由发布lib的调试版本,它只会使二进制分发版膨胀。但是在调试中构建的客户端代码拒绝链接到我的发布库。我以前见过这个问题,但他们似乎没有问正确的问题。我明白这个错误是什么,以及为什么我会得到它(好吧,有点;我不确定到底是什么发出了依赖性。你呢?),但我想知道的是如何消除这种依赖性的发生在我的库中?类似于在使用冲

c++ - std::atomic 的默认值是多少?

我发现在实践中,对于各种C++11/C++14编译器,std::atomic有一个未定义的初始值,就像它是一个"生”类型。也就是说,我们期望对于表达式inta;a可以有任何值。对于表达式std::atomicb;b也可以有任何值。换句话说,std::atomicb;//bisundefined不等于std::atomicb{0};//b==0或到std::atomicb{};//b==0因为在后两种情况下,b被初始化为一个已知值。我的问题很简单:在C++11或C++14规范中的什么地方记录了这种行为? 最佳答案 [atomics.t

c++ - 在 Google 测试失败输出消息中自定义实际/预期的 "Value of"字符串

我从GoogleTest得到以下输出单元测试:UnitTests.cc:56:FailureValueof:LineSegment2i(Vector2i(-10,0),Vector2i(-10,10)).toLine()Actual:24-byteobjectExpected:Line(10,3.14159265358979323846)Whichis:24-byteobject[FAILED]LineSegmentTests.toLine(1ms)那个十六进制输出字符串不是很有用。有什么我可以添加到Line的吗?类(相等性测试失败)在这种情况下提供更多有用的错误?有问题的类已经覆盖了

c++ - std::atomic<uint_least8_t> 行为

在一个系统上:typedefunsignedcharuint8_t;typedefunsignedshortuint16_t;std::atomic::is_always_lock_free//=>falsestd::atomic::is_always_lock_free//=>true据我了解,类型std::atomic将是8位并且不是无锁的。如果是这样,如果我想要一个至少8位且始终无锁的原子类型,我应该怎么写?(假设存在这种类型)是否有比以下更好的选择:std::atomic::is_always_lock_free,uint8_t,uint16_t>::type>(为简单起见,我

c++ - 什么是 "a value not associated with an object"?

C++11和C++14标准(以及工作草案)在§3.10.1中说:Aprvalue(“pure”rvalue)isanrvaluethatisnotanxvalue.[Example:Theresultofcallingafunctionwhosereturntypeisnotareferenceisaprvalue.Thevalueofaliteralsuchas12,7.3e5,ortrueisalsoaprvalue.—endexample]和Anrvalue(socalled,historically,becauservaluescouldappearontheright-han

c++ - 技巧 : filling array values using macros (code generation)

AreC++TemplatesjustMacrosindisguise?我正在阅读上述主题,突然想到一个想法:为什么不尝试编写一些可以在我们的实际代码中使用的棘手宏,(不仅仅是作为在现实生活中无用的谜题)?所以首先想到的是:用宏填充数组值:intf(int&i){return++i;}#definee100r5(m20)#definem20m5,m5,m5,m5#definem5r5(e1)#definee1f(i)//avoiding++irighthere,toavoidUB!#definer5(e)e,e,e,e,eintmain(){inti=0;//thisisusedint

c++ - ldr [pc, #value] 的奇怪行为

我正在调试一些C++代码(ARM平台上的WinCE6),我发现有些行为很奇怪:4277220Cmovr3,#0x93,3042772210strr3,[sp]42772214ldrr3,[pc,#0x69C]42772218ldrr2,[pc,#0x694]4277221Cmovr1,#042772220ldrr0,[pc,#0x688]42772214ldrr3,[pc,#0x69C]行用于从.DATA部分获取一些常量,至少我是这么认为的。奇怪的是,根据代码r2应该从地址pc=0x42772214+0x69C=0x427728B0填充内存,但根据它从0x427728B8(8字节+)加

c++ - 如何正确使用 std::atomic_signal_fence()?

cppreference.com将此函数记录为“线程与在同一线程中执行的信号处理程序之间的栅栏”。但是网上没找到例子。我想知道以下伪代码是否正确说明了std::atomic_signal_fence()的功能:intn=0;SignalObjects;voidthread_1(){s.wait();std::atomic_signal_fence(std::memory_order_acquire);assert(1==n);//neverfires???}voidthread_2(){n=1;s.signal();}intmain(){std::threadt1(thread_1);