[Qt-interest] Documentation on Mutex, Semaphore and Critical Section with Qt?

Matthias Pospiech matthias.pospiech at gmx.de
Fri Jan 29 15:09:53 CET 2010


Thiago Macieira schrieb:
> Em Sexta-feira 29 Janeiro 2010, às 11:05:34, Matthias Pospiech escreveu:
>   
>> For example I want to reinitialize a class member pointer, it could also 
>> be a global singleton.
>> I have to make sure that not a single class in acting on that pointer 
>> when I delete it. So I have to do something like
>> LockAllUses + WaitForAllUsesToFinish + reinitialize + ReleaseLockedUses
>>     
>
> You're describing QReadWriteLock locking for writing.
>   
The name is misleading since it does not seam to have anything to do 
with my problem, but on a close look it seems close to it.
Let me say how I understand it:

My reinitialization it would look like this

reinit()
{
    lock.lockForWrite();
    if (device) {
        delete device;
    }
    device = new DeviceClass;
    lock.unlock();
}


an in all accessing functions I would write:

access()
{
    lock.lockForRead();
    device->doSomething();
    lock.unlock();
}

and if now I am entering reinit then any of my 10-100 access functions
first have to finish or are locked until ever access function is 
unlocked or locked
before the reinit lock allows to be processed. After the write is 
unlocked all read lock are freed again.

The only thing I am missing is the ability to not lock but skip the 
function. For example I have access functions which are
only called on demand and must be processed and others which are called 
with a timer and are only updating the current status.
Here the timer should not lead to several locks. Instead this should 
result in a simple leaving of the function.
But this would require to look at the status of the lock which seams not 
possible?

In some cased the reinit and access is not in different threads. 
Parallel processing nevertheless happens because timers and user 
interaction comes simulateously.
Would the same be possible without threads?



More information about the Qt-interest-old mailing list