我遇到了一个(基本)自旋锁互斥锁的问题,它似乎无法按预期工作。
4 个线程正在递增一个受此互斥体保护的非原子计数器。 结果与预期结果不匹配,这使得互斥锁似乎被破坏了。
示例输出:
result: 2554230
expected: 10000000
在我的环境中,它发生在以下条件下:
flag是 std::atomic<bool> , 任何其他内容,例如 std::atomic<int>或 std::atomic_flag (与 test_and_set )工作正常。
使用 gcc 6.3.1 和 -O3 在 X86_64 上编译标记
我的问题是,什么可以解释这种行为?
#include <iostream>
#include <vector>
#include <atomic>
#include <thread>
#include <mutex>
#include <assert.h>
class my_mutex {
std::atomic<bool> flag{false};
public:
void lock()
{
while (flag.exchange(true, std::memory_order_acquire));
}
void unlock()
{
flag.store(false, std::memory_order_release);
}
};
my_mutex mut;
static int counter = 0;
void increment(int cycles)
{
for (int i=0; i < cycles; ++i)
{
std::lock_guard<my_mutex> lck(mut);
++counter;
}
}
int main()
{
std::vector<std::thread> vec;
const int n_thr = 4;
const int n_cycles = 2500000;
for (int i = 0; i < n_thr; ++i)
vec.emplace_back(increment, n_cycles);
for(auto &t : vec)
t.join();
std::cout << " result: " << counter << std::endl;
std::cout << "expected: " << n_cycles * n_thr << std::endl;
}
根据 Voo 的请求,这里是 increment() 的程序集输出..
$ g++ -O3 increment.cpp
$ gdb a.out
Reading symbols from a.out...done.
(gdb) disassemble increment
Dump of assembler code for function increment(int):
0x0000000000401020 <+0>: mov 0x20122a(%rip),%ecx # 0x602250 <_ZL7counter>
0x0000000000401026 <+6>: test %edi,%edi
0x0000000000401028 <+8>: mov $0x1,%edx
0x000000000040102d <+13>: lea (%rdi,%rcx,1),%esi
0x0000000000401030 <+16>: jle 0x401058 <increment(int)+56>
0x0000000000401032 <+18>: nopw 0x0(%rax,%rax,1)
0x0000000000401038 <+24>: mov %edx,%eax
0x000000000040103a <+26>: xchg %al,0x20120c(%rip) # 0x60224c <mut>
0x0000000000401040 <+32>: test %al,%al
0x0000000000401042 <+34>: jne 0x401038 <increment(int)+24>
0x0000000000401044 <+36>: add $0x1,%ecx
0x0000000000401047 <+39>: cmp %ecx,%esi
0x0000000000401049 <+41>: mov %ecx,0x201201(%rip) # 0x602250 <_ZL7counter>
0x000000000040104f <+47>: movb $0x0,0x2011f6(%rip) # 0x60224c <mut>
0x0000000000401056 <+54>: jne 0x401038 <increment(int)+24>
0x0000000000401058 <+56>: repz retq
End of assembler dump.
最佳答案
您的代码是正确的。这是一个 bug 80004 - [6 回归] 使用 std::memory_order_acquire 将非原子负载移至原子负载之前
关于c++ - 原子 bool 无法保护非原子计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42147192/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
GivenIamadumbprogrammerandIamusingrspecandIamusingsporkandIwanttodebug...mmm...let'ssaaay,aspecforPhone.那么,我应该把“require'ruby-debug'”行放在哪里,以便在phone_spec.rb的特定点停止处理?(我所要求的只是一个大而粗的箭头,即使是一个有挑战性的程序员也能看到:-3)我已经尝试了很多位置,除非我没有正确测试它们,否则会发生一些奇怪的事情:在spec_helper.rb中的以下位置:require'rubygems'require'spork'