Sam*_*Sam 6 c++ stl atomic c++11
在新C++11标准中,许多原子操作在"强/弱"对中定义:
template< class T >
bool atomic_compare_exchange_weak( std::atomic<T>* obj,
T* expected, T desired );
template< class T >
bool atomic_compare_exchange_weak( volatile std::atomic<T>* obj,
T* expected, T desired );
Run Code Online (Sandbox Code Playgroud)
我知道弱者可能会更快,但偶尔会失败,所以需要将它放入while如下的循环中(取自cppreference):
void append(list* s, node* n)
{
node* head;
do {
head = s->head;
n->next = head;
} while(! std::atomic_compare_exchange_weak(s->head, head, n));
}
Run Code Online (Sandbox Code Playgroud)
但
弱行动到底是做什么的?为什么他们有时会失败,为什么他们会更快?(我会喜欢一些核心微处理器架构细节.)
什么时候弱的形式更好,什么时候推荐强者?
| 归档时间: |
|
| 查看次数: |
182 次 |
| 最近记录: |