Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 when increment the atomic variable, and assign it to other value, is it atomic operation?

i'm confused about the atomic operation on c++11,

i know the atomic variable self increment is atomic operation,

but i use the assignment to other value, just doubt it.

the code just like:

//....
static std::atomic<int> i; // global variable
//....
// in the thread
int id = ++i; 

when using the assignment at different threads, is the id unique value?

the test code:

#include <thread>
#include <mutex>
#include <atomic>
#include <iostream>

class A {
public:
    static int idGenerator;
    static std::mutex m;
    A () {
        // i know this operation will keep the id_ is unique
        std::lock_guard<std::mutex> lock(m);
        id_ = ++idGenerator; 
    }
   void F(std::string name) {
         std::cout << name << " " << id_ << std::endl;
    }
private:
    int id_;
};
int A::idGenerator = 0;
std::mutex A::m;

class B {
public:
    static int idGenerator;
    B () {
        // after self increment, is the assignment atomic? 
        id_ = (++idGenerator);
    }
   void F(std::string name) {
         std::cout << name << " " << id_.load() << std::endl;
    }
private:
    std::atomic<int> id_;
};
int B::idGenerator = 0;


void funcA() {
    A a2;
    a2.F("a2");
}

void funcB() {
    B b2;
    b2.F("b2");
}

int main() {
    A a1;
    B b1;
    std::thread t1(&funcA);
    std::thread t2(&funcB);
    a1.F("a1");
    b1.F("b1");

    t1.join();
    t2.join();
    return 0;
}

there are three threads,

A class use lock_guard keep unique.

B class just use atomic operation, and assign to the variable

like image 584
kaka_ace Avatar asked Sep 03 '14 02:09

kaka_ace


People also ask

Is increment operator Atomic?

The increment-memory machine instruction on an X86 is atomic only if you use it with a LOCK prefix. x++ in C and C++ doesn't have atomic behavior.

What is an atomic operation example?

An example of atomic operation is instruction execution, usually an instruction feed to the execution unit can't be stopped in the middle. Yet, a statement in high level language results in multiple instructions. It is the root cause of non-atomic operations.

Is ++ an operation atomic?

On objects without an atomic type, standard never defines ++ as an atomic operation. C11 defines atomic types in stdatomic. h. If you have an object with an atomic type, a postfix and prefix operators ++ will define an atomic operation as: read-modify-write operation with memory_order_seq_cst memory order semantics.

What is atomic operation in operating system?

Atomic operations are sequences of instructions that guarantee atomic accesses and updates of shared single word variables. This means that atomic operations cannot protect accesses to complex data structures in the way that locks can, but they provide a very efficient way of serializing access to a single word.


2 Answers

The specification of the atomic increment functions give a crucial insight into their behaviour - from http://en.cppreference.com/w/cpp/atomic/atomic/operator_arith for Integral T types:

T operator++();
T operator++() volatile;
T operator++( int );
T operator++( int ) volatile;

Notice they return a T by value, and never return the usual T& from a pre-increment. For that reason, the "read" of post-incremented value is not a second distinct operation, and is part of the atomic increment operation itself.

See also the "Return Value" and "Note" text on the above-linked page.

like image 182
Tony Delroy Avatar answered Sep 20 '22 14:09

Tony Delroy


static std::atomic<int> i; // global variable
// in the thread
int id = ++i; 

when using the assignment at different threads, is the id unique value?

Yes. C++ atomic variables ensure that ++i will be evaluated atomically, so each values of id on different threads are unique.

The expression id = ++i; is executed following steps.

  1. atomically increment i, and sub-expression(++i) is evaluated post-increment value.
  2. assign "evaluated value" to id. (this step is non-atomically)
like image 44
yohjp Avatar answered Sep 19 '22 14:09

yohjp