Is volatile int in C as good as std::atomic of C++0x?

后端 未结 6 713
醉话见心
醉话见心 2020-12-14 17:55

I need to have atomic variables in my program. Previously I was using std::atomic, but the platform in which I\'m working now does not have a g++ com

6条回答
  •  醉梦人生
    2020-12-14 18:58

    There's a good summary of the diffs here, from Herb Sutter. In summary (cut and paste):

    To safely write lock-free code that communicates between threads without using locks, prefer to use ordered atomic variables: Java/.NET volatile, C++0x atomic, and C-compatible atomic_T.

    To safely communicate with special hardware or other memory that has unusual semantics, use unoptimizable variables: ISO C/C++ volatile. Remember that reads and writes of these variables are not necessarily atomic, however.

提交回复
热议问题