C++原子操作和无锁编程

1.

  linux内核的自旋锁: 参考

  参考

  Qt里的原子操作

  gcc内置的cas操作: 官方  参考

  futex的设计与实现: 参考

  c++的atomic是真正的原子吗? 参考

// cas: g++ test.cc -lpthread -march=nocona -mtune=generic

// resolve cas ABA question: http://www.360doc.com/content/14/0811/10/1073512_400983810.shtml

// impliment a spinlock: https://www.orcode.com/question/484402_k5a4f5.html
// if (__sync_bool_compare_and_swap(&cas_count, old, old + 1))

// https://blog.csdn.net/qq_34595352/article/details/89139378

// http://c.dovov.com/12550/__sync_val_compare_and_swap-vs-__sync_bool_compare_and_swap.html
// http://git.musl-libc.org/cgit/musl/tree/src/thread/pthread_spin_trylock.c?id=afbcac6826988d12d9a874359cab735049c17500


#if 0

no lock code:
https://blog.csdn.net/weixin_43580319/article/details/117048449

spin lock:
https://zhuanlan.zhihu.com/p/80727111

cas detail:
https://www.cnblogs.com/wzjhoutai/p/6838104.html
https://www.cnblogs.com/luconsole/p/4944304.html

memory order:
https://en.cppreference.com/w/cpp/atomic/atomic_flag

futex:
https://blog.csdn.net/y33988979/article/details/82252266
https://blog.csdn.net/mitushutong11/article/details/51336136

2. C++里的锁以及原子操作等

#include <chrono>
#include <atomic>
#include <mutex>

  std::atomic_flag 参考

  std::atomic<int> count(0); // c++17
  std::atomic<bool> ready (false);

3. volitile关键字

  参考

  

 

posted @ 2021-06-24 14:14  AlexOne  阅读(147)  评论(0)    收藏  举报