Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing std::atomic_bool?

I want to use std::atomic_bool because I want to have a boolean which is supposed to be accessed by different threads.

It's a static member Variable. The Problem is that I want to initialize it with false as the first state. Normally I would do it like that: std::atomic_bool World::mStopEvent = false;

But the problems seems to be that it doesn't take false as the constructor. So how I am supposed to initialize such a variable? I am using VS 2012.

like image 813
Sapd Avatar asked Apr 01 '13 20:04

Sapd


People also ask

How do you declare an atomic variable in 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.

Is STD atomic thread safe?

Yes, it would be threadsafe. Assuming of course there are no bugs in the std::atomic implementation - but it's not usually hard to get right. This is exactly what std::atomic is meant to do.

What is std :: atomic in C++?

Each instantiation and full specialization of the std::atomic template defines an atomic type. Objects of atomic types are the only C++ objects that are free from data races; that is, if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined.


2 Answers

This is a known issue in Visual Studio 2012 (known as VC11), you should vote on the existing Connect item so that Microsoft knows it affects more folks as they've deferred the fix.

Hi,

Thanks for reporting this bug. I'm Microsoft's maintainer of the STL, and I wanted to let you know that while this bug remains active in our database, it won't be fixed in VC11 RTM (VS 2012 RTM). All bugs are important to us, but some are more severe than others and rise to the top of our priority queue.

I'm copying-and-pasting this response across all of the STL's active Connect bugs, but the following terse comments apply specifically to your bug:

  • Yep, we're missing these constructors on atomic_bool, atomic_int, etc. (atomic<bool>, atomic<int>, etc. have them). 29.5 [atomics.types.generic]/7 says "There shall be named types corresponding to the integral specializations of atomic, as specified in Table 145, and a named type atomic_bool corresponding to the specified atomic<bool>. Each named type is a either typedef to the corresponding specialization or a base class of the corresponding specialization. If it is a base class, it shall support the same member functions as the corresponding specialization." which makes me really want to use typedefs (1 type is always simpler than 2 types), but I'll need to see if that would introduce any other issues.

I can't promise when we'll be able to resolve this bug, but we hope to do so as soon as possible (and I'll send another response when that happens) - our first opportunity will be the "out of band" release between VC11 and VC12 that Herb Sutter announced at the GoingNative 2012 conference.

Note: Connect doesn't notify me about comments. If you have any further questions, please E-mail me.

Stephan T. Lavavej Senior Developer - Visual C++ Libraries [email protected]

Basically, you'll need to use std::atomic<T> for now.

like image 62
user7116 Avatar answered Sep 28 '22 14:09

user7116


Try this:

atomic_bool my_bool = ATOMIC_VAR_INIT(false); 

http://en.cppreference.com/w/cpp/atomic/ATOMIC_VAR_INIT

like image 30
andrewrk Avatar answered Sep 28 '22 14:09

andrewrk