Search code examples
c++multithreadingc++11concurrencystdatomic

How do you use a std::atomic<bool> to ensure mutual exclusion in a code block?


I have a function that accesses(reads and writes to) a std::atomic<bool> variable. I'm trying to understand the order of execution of instructions so as to decide whether atomic will suffice or if I've got to use mutexes here. The function is given below -

// somewhere member var 'executing' is defined as std::atomic<bool>`

int A::something(){
    
    int result = 0;
    // my intention is only one thread should enter next block
    // others should just return 0
    if(!executing){
        executing = true;

        ...
        // do some really long processing
        ...
       
        result    = processed;
        executing = false;
    }
    
    return result;
}

I've read this page on cppreference which mentions -

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)

and on Memory model page the following is mentioned -

When an evaluation of an expression writes to a memory location and another evaluation reads or modifies the same memory location, the expressions are said to conflict. A program that has two conflicting evaluations has a data race unless either

  • both conflicting evaluations are atomic operations (see std::atomic)

  • one of the conflicting evaluations happens-before another (see std::memory_order)

If a data race occurs, the behavior of the program is undefined.

and slight below it reads -

When a thread reads a value from a memory location, it may see the initial value, the value written in the same thread, or the value written in another thread. See std::memory_order for details on the order in which writes made from threads become visible to other threads.


This is slightly confusing to me, which one of above 3 statements are actually happening here?

When I perform if(!executing){ is this instruction an atomic instruction here? and more important - is it guaranteed that no other thread will enter that if loop if one two threads will enter that if body since first one will set executing to true?

And if something's wrong with the mentioned code, how should I rewrite it so that it reflects original intention..


Solution

  • Can you use a std::atomic_bool to ensure mutual exclusion in a code block?

    Yep. std::atomic_bool is sufficient to implement a spinlock, which is simpler (although also less general and a worse default) than the std::mutex in the other answer, and still more complicated than you actually need.

    Your problem is here:

    When I perform if(!executing){ ... is it guaranteed that no other thread will enter that if loop if one two threads will enter that if body since first one will set executing to true?

    Both the load (for comparison) and the store are individually atomic, but they're not a single atomic operation, which literally means they're divisible (by another operation in a different thread).

    However, there is an atomic load+compare+store for exactly this purpose: compare_exchange.

    Your code should be something like:

    int A::something() {
        
        int result = 0;
    
        // if executing is false,
        // atomically set it to true
        bool expect_executing = false;
        if(executing.compare_exchange_strong(
            expect_executing, true)) {
    
            // enter the branch only if the exchange succeeded
            // (executing was false and is now true)
            // ... do some really long processing ...
           
            result = processed;
            executing = false;
        }
        
        return result;
    }
    

    You can do something similar with std::atomic_flag::test_and_set, but I stuck with your existing type.

    You may also be able to weaken the default (sequential consistency) ordering.