Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::atomic vs. Boost atomic

In my application, I have an int and a bool variable, which are accessed (multiple write/read) by multiple threads. Currently, I am using two mutexes, one for int and one for bool to protect those variables.

I heard about using atomic variables and operators to write lock-free multi-thread program. My questions are

  1. What's the definition of atomic variables and operators?
  2. What's the main difference between std::atomic and boost/atomic.hpp? Which one is more standard or popular?
  3. Are these libraries platform-dependent? I am using gnu gcc 4.6 on Linux at the moment, but ideally it shall be cross-platform. I heard that the definition of "atomic" actually depends on the hardware as well. Can anyone explain that as well?
  4. What's the best way to share a bool variable among multiple threads? I would prefer not to use the "volatile" keyword.

Are these code thread-safe?

double double_m; // double_m is only accessed by current thread. std::atomic<bool> atomic_bool_x; atomic_bool_x = true && (double_m > 12.5);  int int_n; // int_n is only accessed by current thread. std::atomic<int> atomic_int_x; std::atomic<int> atomic_int_y; atomic_int_y = atomic_int_x * int_n; 
like image 684
2607 Avatar asked Mar 04 '12 02:03

2607


People also ask

What is boost atomic?

Atomic is a library that provides atomic data types and operations on these data types, as well as memory ordering constraints required for coordinating multiple threads through atomic variables.

Is atomic thread safe C++?

In order to solve this problem, C++ offers atomic variables that are thread-safe. The atomic type is implemented using mutex locks. If one thread acquires the mutex lock, then no other thread can acquire it until it is released by that particular thread.

What is std :: atomic C++?

Each instantiation and full specialization of the std::atomic template defines an atomic type. If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory model for details on data races).

Is ++ an atomic operation C++?

x++ in C and C++ doesn't have atomic behavior.


2 Answers

I'm not an expert or anything, but here's what I know:

  1. std::atomic simply says that calling load and store (and a few other operations) concurrently is well-defined. An atomic operation is indivisible - nothing can happen 'in-between'.
  2. I assume std::atomic is based off of boost::atomic. If you can, use std, otherwise use boost.
  3. They are both portable, with the std being completely so, however your compiler will need to support C++11
  4. Likely std::atomic_bool. You should not need to use volatile.

Also, I believe load/store differs from operator=/operator T only load/store are atomic.

Nevermind. I checked the standard and it appears that the operators are defined in terms of load/store/etc, however they may return different things.

Further reading:

  • http://en.cppreference.com/w/cpp/atomic/atomic
  • C++11 Standard
  • C++ Concurrency in Action
like image 53
Pubby Avatar answered Oct 03 '22 03:10

Pubby


Volatile is orthogonal to what you use to implement atomics. In C++ it tells the compiler that certain it is not safe to perform optimizations with that variable. Herb Sutters lays it out:

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.

Finally, to express a variable that both has unusual semantics and has any or all of the atomicity and/or ordering guarantees needed for lock-free coding, only the ISO C++0x draft Standard provides a direct way to spell it: volatile atomic.

(from http://drdobbs.com/article/print?articleId=212701484&siteSectionName=parallel)

like image 23
Andrew Khosravian Avatar answered Oct 03 '22 03:10

Andrew Khosravian