multithreading - have QThread something like volatile member in Java? -


this question has answer here:

i have 5 qthread working . have 1 integer variable want share among them ! work 1 thread on integer important thread . how can share between them ?!

use qmutex qmutexlocker around variable.

edit:

qmutex * mutex = new qmutex(); int shared_integer = 0;  void func1() {     int temp;         // lots of calculations     temp = final_value_from_calculations;      // save shared integer     {         qmutexlocker locker(mutex);// thread waits until mutex unlocked.         qdebug() << "mutex locked!";         // access shared variable         shared_integer = temp;          // if had reason return here, mutex locker         // unlock putex properly!         // return; // locker's destructor gets called , mutex gets unlocked      }// lockers's destructor gets called , mutex gets unlocked     qdebug() << "mutex unlocked!";  }  void func2() {     qmutexlocker locker(mutex);     qdebug() << shared_integer; } 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -