知りたい

  • atomicとは
  • ユースケース

メモ

  • atomic変数に対する操作は、他のスレッドによって割り込まれることはない

load-acuire

atomicVar.load()は以降の命令をオーバーラップしない。

std::atomic<int> atomicVar;
atomicVar.load(std::memory_order_acquire);
std::cout << “hoge” << std::endl;

store-release

a = 0は、atomicVar.store()をオーバーラップしない。

int a = 0;
atomicVar.store(123, std::memory_order_release);

exchange()

スピンロックの実装に使える。

参考:
https://stackoverflow.com/questions/7007834/what-is-the-use-case-for-the-atomic-exchange-read-write-operation

ロックフリースタックの実装

参考:
https://lumian2015.github.io/lockFreeProgramming/lock-free-stack.html

ロックフリーキューの実装

参考:
https://enqueuezero.com/lock-free-queues.html#context

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.53.8674&rep=rep1&type=pdf

CASと通常のインクリメントの違い

  • CAS: 他のスレッドの処理が終わるのを待つ処理に使う
  • インクリメント: 参照カウンタとして使う

カウンタ操作は排他なので後者は前者に含まれていると言えるが…。

参考:違いとユースケース

サンプルコードリンク集