Quantcast
Channel: Usage of an atomic variable within threads - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Usage of an atomic variable within threads

$
0
0

Consider a program with three threads A,B,C.

They have a shared global object G.

I want to use an atomic variable(i) inside G which is written by Thread B and Read by A.

My approach was:

declare i in G as:

std::atomic<int> i;

write it from thread B using a pointer to G as:

G* pG; //this is available inside A and B pG->i = 23;

And read it from thread A using the same way.

int k = pG->i;

Is my approach correct if these threads try to access this variable simultaneously.?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images