Re: QMutex protection type??
QMutex not protects variables, it suspends program/thread execution. So each call to your function is not preceded by an attempt to execute a semaphore lock, regardless of its condition.
Re: QMutex protection type??
QMutex doesn't lock any variables. It makes sure no two threads have the mutex locked at the same time. If you want to protect some variable you have to make sure all calls to it are within critical sections protected by the same mutex.
Re: QMutex protection type??
emm.. just want to make sure.. if thread one call for "somefunction" which need to access variable j.. thread two will have to wait until thread one finish execute the function due to lock command for variable j or the function right??
Re: QMutex protection type??
If you lock the mutex both in the function and in the other place then yes. Other than that -- no, the mutex doesn't care what is inside the critical section it protects.
Re: QMutex protection type??
Quote:
Originally Posted by
snow_starzz
emm.. just want to make sure.. if thread one call for "somefunction" which need to access variable j.. thread two will have to wait until thread one finish execute the function due to lock command for variable j or the function right??
In your sample code you always call the functions from within a locked code block, so yes, only one thread can ever be executing them with the other one waiting.
QMutex only provides you with means to make sure only one thread at a time can get past lock(). What is protected by that depends on what you as the programm do with the things accessed afterwards, i.e. you need to make sure that access to variables is always happening between lock and unlock.
Cheers,
_
Re: QMutex protection type??
thanks all... i guess i have to revised back my program flow.. there are several thread that using the same global function in different lock's block but the lock is also different.. =(