資源簡介
c++11多線程編程之原子庫的使用方式,給出了多線程情況下原子數的不可改變性實例
代碼片段和文件信息
//?atomic::compare_exchange_weak?example:
#include????????//?std::cout
#include??????????//?std::atomic
#include??????????//?std::thread
#include??????????//?std::vector
//?a?simple?global?linked?list:
struct?Node?{?int?value;?Node*?next;?};
std::atomic?list_head(nullptr);
void?append(int?val)?{?????//?append?an?element?to?the?list
Node*?newNode?=?new?Node{?vallist_head?};
//?next?is?the?same?as:?list_head?=?newNode?but?in?a?thread-safe?way:
while?(!list_head.compare_exchange_weak(newNode->next?newNo
- 上一篇:譚浩強c++課本第一章習題答案源碼
- 下一篇:多線程編程之future庫使用
評論
共有 條評論