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

Andre Somers andre at familiesomers.nl
Fri Jan 29 15:20:48 CET 2010


Matthias Pospiech wrote:
> 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();
> }
>
>   
Without going into your locking/unlocking scheme (I'll leave that to the 
experts): I'd strongly advise you not to lock/unlock your mutexes 
manually! You are bound to forget one at one point, run into an 
exception that prevents the unlock or something like it. Instead, use a 
QMutexLocker (or some other resource managing object that does the same).

André




More information about the Qt-interest-old mailing list