我在考虑原子变量是否可以加载获取-释放对中的旧值。 假设我们有原子变量 x,我们用释放语义存储该变量,然后用获取语义加载它,理论上是否可以读取旧值?
std::atomic<int> x = 0;
void thread_1()
{
x.store(1, std::memory_order_release);
}
void thread_2()
{
assert(x.load(std::memory_order_acquire) != 0);
}
如果函数线程 1 在线程 2 加载 x 时完成(因此存储了新值),线程 2 是否可以从 x 加载旧值?换句话说,如果对 x 的实际存储在加载之前完成,断言是否有可能触发?
据我从网上的文章中了解到这是可能的,但我不明白为什么。 store to x 生成的内存栅栏保证清空存储缓冲区,而从 x 加载中获取内存栅栏保证使缓存行无效,因此它必须读取最新值。
已添加
这是否意味着 acquire-release 本身没有任何强制顺序?只有在发布之前完成的任何事情都会在发布之前发生,而在获取之后完成的所有事情都会在它之后发生,所以获取-释放对强制对其他操作进行排序(为什么??)。我做对了吗?这是否意味着在下面的代码中断言保证不会触发
std::atomic<int> x = 0;
std::atomic<int> y = 0;
void thread_1()
{
y.store(1, std::memory_order_relaxed);
x.store(1, std::memory_order_release);
}
void thread_2()
{
x.load(std::memory_order_acquire);
assert(y.load(std::memory_order_relaxed) != 0);
}
如果线程 1 已经完成存储,当然会再次发生。如果我们将 x.load 替换为 while (x.load() == 0) 这将 100% 起作用,但我不知道是什么原因导致它起作用。
如果我用下面的代码替换代码会怎么样
std::atomic<int> x = 0;
void thread_1()
{
x.exchange(1, std::memory_order_acq_rel);
}
void thread_2()
{
assert(x.exchange(0, std::memory_order_acq_rel) != 0);
}
它会改变什么吗?
谢谢。
最佳答案
您可以将具有释放/获取内存顺序的存储/加载函数视为以下伪代码:
template<class T>
struct weak_atomic
{
void store(T newValue)
{
ReleaseBarrier();
m_value = newValue;
}
T load()
{
T value = m_value;
AcquireBarrier();
return value;
}
volatile T m_value;
}
你说
Memory fence generated by store to x guaranties to empty store buffer
据我了解,释放内存屏障将导致 CPU 刷新其存储缓冲区,但它将在之前将新值应用于 x。因此,似乎可以通过另一个 CPU 从 x 读取旧值。
无论如何,弱原子是一个非常复杂的领域。在继续进行无锁编程之前,请确保您了解内存屏障。
已添加
看来您仍然对内存障碍感到困惑。这是一个非常常见的用法示例。
volatile int x;
volatile bool ok;
void thread_1()
{
x = 100;
ok = true;
}
void thread_2()
{
if (ok)
{
assert(x == 100);
}
}
由于乱序执行,您可能会得到以下序列:
thread 1 sets ok to true
thread 2 checks ok is true and reads some garbage from x
thread 1 sets x to 100 but it is too late
另一种可能的顺序:
thread 2 reads some garbage from x
thread 2 checks for ok value
我们可以通过释放和获取内存屏障来解决这个问题。
volatile int x;
volatile bool ok;
void thread_1()
{
x = 100;
ReleaseBarrier();
ok = true;
}
void thread_2()
{
if (ok)
{
AcquireBarrier();
assert(x == 100);
}
}
ReleaseBarrier() 保证内存写入不能跳过屏障。
这意味着 ok 仅在 x 已包含有效值时才设置为 true。
AcquireBarrier() 保证内存读取不会跳过屏障。
这意味着只有在检查ok 状态后,才会读取x 的值。
这就是发布/获取对的用途。我们可以用我的 weak_atomic 重写这个例子。
volatile int x;
weak_atomic<bool> ok;
void thread_1()
{
x = 100;
ok.store(true);
}
void thread_2()
{
if (ok.load())
{
assert(x == 100);
}
}
关于c++ - 乱序执行的获取-释放对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4442934/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur