Atomic variables other than c++11 and boost

128 views Asked by At

I currently require an atomic variable to cleanly exit my multi threaded program. On receiving a signal(such as SIGINT), the atomic variable is set to false, my threads exit, join the main program and I have a clean exit.

I however cannot use C++11 (building with C++ 11 causes too many compile errors).
My current boost library(ver 1.51) dosen't support atomic variables.

  1. Do I have any other options available?
  2. Do I even require an atomic variable, since it is set only at one location and read everywhere else
3

There are 3 answers

2
marom On

So far I know that version of boost library should support atomic variables.

Give a look in boost/interprocess/detail/atomic.hpp

0
Tsyvarev On
  1. Do I even require an atomic variable, since it is set only at one location and read everywhere else?

For a flag, which is set in signal handler, it is sufficient to use volatile bool (or volatile int) type. Atomic adds nothing in that case.

0
MikeMB On

Actually, the type you are supposed to use in signal handlers in C++03 as well as in c++11/14 is sig_atomic_t.

Signal handlers have even stronger requirements on allowed types than threads, because they must not block.

Also, while pre c++11 didn't have a multithreaded memory model, it was possible to write threadsafe code (e.g. according to the POSIX standard) for decades. So if you are using pthreads anyway, chances are you don't have to worry about what the pure c++03 standard guarantees (or rather what it doesn't guarantee) but you rather should look at the POSIX and/or gcc documentation.