C++ boost::atomic与boost::可选不同行为与boost 1.55和1.58 structfoo { void updateMin(常量int和值); boost::atomic m_min;//多线程访问 }; void Foo::updateMin(常量int和值) { auto currentMin=m_min.load(boost::memory_order_released); 内特纽明; 做 { 如果(!currentMin) newMin=值; 其他的 { newMin=std::min(值,currentMin.get()); 如果(newMin==currentMin) 打破 } }而(!m_min.compare_exchange_弱(currentMin,boost::optional(newMin),boost::memory_order_relaxed)); }
在boost 1.55中,上述代码可以正常工作 当我尝试将boost版本更新为1.58时,compare_exchange_弱系统性地失败,从而导致无限循环 从1.55开始,我就浏览了atomic和optional的更改日志,但是我没有发现任何明显的东西来解释这种行为C++ boost::atomic与boost::可选不同行为与boost 1.55和1.58 structfoo { void updateMin(常量int和值); boost::atomic m_min;//多线程访问 }; void Foo::updateMin(常量int和值) { auto currentMin=m_min.load(boost::memory_order_released); 内特纽明; 做 { 如果(!currentMin) newMin=值; 其他的 { newMin=std::min(值,currentMin.get()); 如果(newMin==currentMin) 打破 } }而(!m_min.compare_exchange_弱(currentMin,boost::optional(newMin),boost::memory_order_relaxed)); },c++,boost,atomic,C++,Boost,Atomic,在boost 1.55中,上述代码可以正常工作 当我尝试将boost版本更新为1.58时,compare_exchange_弱系统性地失败,从而导致无限循环 从1.55开始,我就浏览了atomic和optional的更改日志,但是我没有发现任何明显的东西来解释这种行为 有什么想法吗?比如std::atomic。 boost::optional并不是完全可以复制的,所以您只会得到未定义的行为 顺便说一句,compare\u exchange.*像通过memcmp那样比较对象,因此它也会考虑任何填充
有什么想法吗?比如
std::atomic
。
boost::optional
并不是完全可以复制的,所以您只会得到未定义的行为
顺便说一句,
compare\u exchange.*
像通过memcmp
那样比较对象,因此它也会考虑任何填充字节。类似于std::atomic
。
boost::optional
并不是完全可以复制的,所以您只会得到未定义的行为
顺便说一下,compare\u exchange.*
将对象与memcmp
进行比较,因此它还将考虑任何填充字节
struct Foo
{
void updateMin(const int& value);
boost::atomic<boost::optional<int>> m_min; //multi-thread access
};
void Foo::updateMin(const int& value)
{
auto currentMin = m_min.load(boost::memory_order_relaxed);
int newMin;
do
{
if (!currentMin)
newMin = value;
else
{
newMin = std::min(value, currentMin.get());
if (newMin == currentMin)
break;
}
} while (!m_min.compare_exchange_weak(currentMin, boost::optional<int>(newMin), boost::memory_order_relaxed));
}