multithreading - have QThread something like volatile member in Java? -
this question has answer here:
- sharing data across qt threads 1 answer
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
Post a Comment