multithreading - Reading a Global Variable from a Thread and Writing to that Variable from another Thread -


my program has 2 threads , int global variable. 1 thread reading variable , other thread writing variable. should use mutex lock in situation.

these functions executing 2 threads simultaneously , repetitively in program.

void thread1() {     if ( condition1 )         ivariable = 1;     else if ( condition2 )         ivariable = 2; }  void thread2() {     if ( ivariable == 1)         //do     else if ( ivarable == 2 )         //do thing  } 

if don't use synchronization entirely unpredictable when 2nd thread sees updated value. ranges somewhere between handful of nanoseconds , never. never outcome being particularly troublesome of course, can happen on x86 processor when don't declare variable volatile , run release build of program. can take long time on processors weak memory model, arm cores. thing don't have worry seeing partially updated value, int updates atomic.

that's can said posted code. fine-grained locking works well.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -