Topic: std::atomic_bool vs. std::atomic<bool>


Author: Scott Meyers <usenet@aristeia.com>
Date: Tue, 24 Nov 2009 14:38:16 CST
Raw View
These types seem to have almost identical interfaces.  The only
difference I see (in terms of functions that may be called -- I didn't
check the semantics of the functions) is that std::atomic<bool> offers

 bool operator=(bool);

and std::atomic_bool does not. (It almost offers that function, but
it's volatile-qualified.)

Can somebody please explain why we need std::atomic_bool instead of
just using std::atomic<bool>?

Thanks,

Scott

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Bo Persson" <bop@gmb.dk>
Date: Tue, 24 Nov 2009 16:18:31 CST
Raw View
Scott Meyers wrote:
> These types seem to have almost identical interfaces.  The only
> difference I see (in terms of functions that may be called -- I
> didn't check the semantics of the functions) is that
> std::atomic<bool> offers
> bool operator=(bool);
>
> and std::atomic_bool does not. (It almost offers that function, but
> it's volatile-qualified.)
>
> Can somebody please explain why we need std::atomic_bool instead of
> just using std::atomic<bool>?
>
> Thanks,
>
> Scott

They were initially added as an offer to the C language committee, for
a common interface.

Seems like the offer wasn't accepted.

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2992.htm#header



Bo Persson



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Anthony Williams <anthony.ajw@gmail.com>
Date: Tue, 24 Nov 2009 16:17:18 CST
Raw View
Scott Meyers <usenet@aristeia.com> writes:

> Can somebody please explain why we need std::atomic_bool instead of
> just using std::atomic<bool>?

As I understand it, the std::atomic_xxx types are the "basic" atomic
types, provided for compatibility with the proposal to the C
committee. std::atomic<xxx> is the general case, which is then specified
to publicly derive from the corresponding atomic_xxx type where there is
one in order to allow interoperability (e.g. you can pass a
std::atomic<int>* to a function taking a std::atomic_int*)

So, the primary reason is potential C compatibility.

Anthony
--
Author of C++ Concurrency in Action | http://www.stdthread.co.uk/book/
just::thread C++0x thread library   | http://www.stdthread.co.uk
Just Software Solutions Ltd         | http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Tue, 24 Nov 2009 16:17:34 CST
Raw View
On 24 Nov., 21:38, Scott Meyers <use...@aristeia.com> wrote:
> These types seem to have almost identical interfaces.  The only
> difference I see (in terms of functions that may be called -- I didn't
> check the semantics of the functions) is that std::atomic<bool> offers
>
>  bool operator=(bool);
>
> and std::atomic_bool does not. (It almost offers that function, but
> it's volatile-qualified.)
>
> Can somebody please explain why we need std::atomic_bool instead of
> just using std::atomic<bool>?

The type atomic_bool is provided as something that would be
compatible with C, because C is also going to develop a
threading library. You can recognize that from the free functions
that are also to provided to act on this type in the typical manner
for C (The atomic_* functions). In C the member functions of
atomic_bool would not exist, of-course. On the other hand,
one might ask, why C++ does specify any members of
the atomic_* types at all, because the atomic template
specializations would seemingly suffice.

HTH & Greetings from Bremen,

Daniel Kr   gler




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Scott Meyers <usenet@aristeia.com>
Date: Tue, 24 Nov 2009 17:42:17 CST
Raw View
Daniel Kr   gler wrote:
> On the other hand,
> one might ask, why C++ does specify any members of
> the atomic_* types at all, because the atomic template
> specializations would seemingly suffice.

So I'll ask:  why?

And why not just define std::atomic_bool to be a typedef for std::atomic<bool>?

Scott


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]