Thread: std::atomic_flag vs std::atomic<bool> question

  1. #1
    Registered User
    Join Date
    Dec 2015

    std::atomic_flag vs std::atomic<bool> question

    I see that the atomic_flag is guaranteed to be lock free. A atomic boolean is not.

    I'm trying to get an understanding when one should be used over the other. If one uses the atomic boolean should a is lock free check be performed? Should the class member functions be used over using an assignment?

  2. #2
    Registered User
    Join Date
    Apr 2006
    In practice, atomic boolean is always lock free, and it has a richer interface, so I'd just use it. More generally, It's more worthwhile to optimize for specific architectures your software will be run on, and not bother with hypothetical platforms that the language supports, especially because you cannot test your code on those platforms.

    With regards to class member functions vs operators, the difference is that the class member functions allow you to pass a weaker memory order. So use either one for sequential consistent memory order, but if you need something weaker, use a class member function.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    And to add to that, unless you really know what you're doing, don't use that member function because if you screw up the memory ordering, you will get very hard to find bugs. Better use the defualt ordering used by the operators and optimize if and only if you absolutely need it. Even IF you do know what you're doing, chances are you'll make a mistake and you'll end up in the same situation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 08:39 AM
  2. New bool class... Question
    By Raigne in forum C++ Programming
    Replies: 30
    Last Post: 04-06-2008, 02:53 PM
  3. question about bool
    By ssharish2005 in forum C Programming
    Replies: 7
    Last Post: 10-17-2005, 08:18 AM
  4. static bool question...
    By jerrykelleyjr in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2005, 09:39 AM
  5. c++ bool question pls help
    By rderiu in forum C++ Programming
    Replies: 9
    Last Post: 01-06-2003, 10:45 PM

Tags for this Thread