multithreading - Qt/C++: Recursive mutex, 'sync zones' and blocking signals -
firstly i'd point out i've looked can't find answer i'm looking for/have got confused overly detailed answers.
i have program uses 2 threads. boolean values need set , read in thread read in thread b.
thread a:
module::module(){ } void module::foo(){ mutex.lock(); bool request = true; mutex.unlock(); } void module::bar(){ mutex.lock(); if (request){ mutex.unlock(); // stuff }else{ mutex.unlock(); } }
thread b:
provider::provider(){ module = new module; // pointer class request 'lives in' } void provider::foo(){ mutex.lock(); if (module->request){ mutex.unlock(); // stuff } }else{ mutex.unlock(); } }
my question might seem rather trivial, it's bugged me. thread cannot read , write @ same time, i'd argue recursive mutex not required a. however, there small possibility foo() , bar() called simultaneous thread b (signals , slots). mean need recursive mutex?
also; there reason not use qt::blockingqueudconnection? colleague argued dangerous sends calling threads sleep until signal has executed slot- not sort of same mutex?
furthermore; seen post regarding structuring mutex (pthread mutex locking variables used in statements). in here mentions making local copies on values. if employ similar thread e.g.
mutex.lock(); requestcopy = request; mutex.lock(); ... if(requestcopy){ // stuff }
will block access of request whereever requestcopy getting used? looking use style in code simplicity not work if read , write in thread?
any great.
from have shown, looks (rewritten)
some module (thread a):
class module { private: bool request = false; qmutex m; public: void set_request(bool b) { qmutexlocker lock(&m); request = b; } bool get_request() { qmutexlocker lock(&m); return request; } void bar() { if (get_request()) { // stuff } } };
thread b:
class provider { public: provider() { module = new module(); } void foo() { if (module->get_request()){ // stuff } } private: module *module; };
if case (and fine way), there no need recursive mutex.
Comments
Post a Comment