I'm implementing Inter-Thread Communication using global variable.
//global var volatile bool is_true = true; //thread 1 void thread_1() { while(1){ int rint = rand() % 10; if(is_true) { cout << "thread_1: "<< rint <<endl; //thread_1 prints some stuff if(rint == 3) is_true = false; //here, tells thread_2 to start printing stuff } } } //thread 2 void thread_2() { while(1){ int rint = rand() % 10; if(! is_true) { //if is_true == false cout << "thread_1: "<< rint <<endl; //thread_2 prints some stuff if(rint == 7) //7 is_true = true; //here, tells thread_1 to start printing stuff } } } int main() { HANDLE t1 = CreateThread(0,0, thread_1, 0,0,0); HANDLE t2 = CreateThread(0,0, thread_2, 0,0,0); Sleep(9999999); return 0; }
Question
In the code above, I use a global var volatile bool is_true
to switch printing between thread_1 and thread_2.
I wonder whether it is thread-safe to use assignment operation here?
In Python anything that involves the execution of a single byte code SHOULD be atomic, but compound assignment does not fit this criteria.
Assignment Operators in C/C++ Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right side operand of the assignment operator is a value.
An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.
This code is not guaranteed to be thread-safe on Win32, since Win32 guarantees atomicity only for properly-aligned 4-byte and pointer-sized values. bool
is not guaranteed to be one of those types. (It is typically a 1-byte type.)
For those who demand an actual example of how this could fail:
Suppose that bool
is a 1-byte type. Suppose also that your is_true
variable happens to be stored adjacent to another bool
variable (let's call it other_bool
), so that both of them share the same 4-byte line. For concreteness, let's say that is_true
is at address 0x1000 and other_bool
is at address 0x1001. Suppose that both values are initially false
, and one thread decides to update is_true
at the same time another thread tries to update other_bool
. The following sequence of operations can occur:
is_true
to true
by loading the 4-byte value containing is_true
and other_bool
. Thread 1 reads 0x00000000.other_bool
to true
by loading the 4-byte value containing is_true
and other_bool
. Thread 2 reads 0x00000000.is_true
, producing 0x00000001.other_bool
, producing 0x00000100.is_true
is now true
and other_bool
is now false
.is_true
is now false
and other_bool
is now true
.Observe that at the end this sequence, the update to is_true
was lost, because it was overwritten by thread 2, which captured an old value of is_true
.
It so happens that x86 is very forgiving of this type of error because it supports byte-granular updates and has a very tight memory model. Other Win32 processors are not as forgiving. RISC chips, for example, often do not support byte-granular updates, and even if they do, they usually have very weak memory models.
no, its not..... you need to use a locking primitive of some sort. Depending on platform, you can either use boost ones, or if going native windows, something like InterlockedCompareExchange.
In fact in your situation you might what to use some of the thread safe event mechanisims so you can 'signal' your other thread to start doing what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With