I have four threads that need to be syncronized. In order to achieve this, I am looking to pass each thread the same bool array. Each thread will change the 'corresponding' value to true once it has reached a certain point in the thread, then constantly check to see if the rest of the values are true. If so, enter the loop... An example
//above thread init stuff
oLock->lockForWrite();
abSync[iThreadNum] = true; //iThreadNum = {0...3} depending on whats set
oLock->unlock();
bool bSynced = false;
while (!bSynced)
{
oLock->lockForRead();
if (abSync[0] && abSync[1] && abSync[2] && abSync[3])
bSynced = true;
oLock->unlock();
}
//below thread run and finish
Does the QReadWriteLock work as described above? Will it in fact lock the variable for the write (as each thread goes through) but not for the read? A quick look at the documentation suggests that QReadWriteLock will only block at lockForRead()
if there is a write lock but not a read lock which is what I want above.
Also, I understand the above will suck down CPU cycles as its going through the loop and that is the wanted behavior. Using a Sleep
is not good enough for our needs.