[Qt-interest] Documentation on Mutex, Semaphore and Critical Section with Qt?
Matthias Pospiech
matthias.pospiech at gmx.de
Fri Jan 29 15:33:50 CET 2010
Sean Harmer schrieb:
>> My reinitialization it would look like this
>>
>> reinit()
>> {
>> lock.lockForWrite();
>> if (device) {
>> delete device;
>> }
>> device = new DeviceClass;
>> lock.unlock();
>> }
>>
>
> Or even better use QWriteLocker locker( &mutex ) that way you are covered at
> every possible exit point from your function including throwing exceptions.
>
>
>> an in all accessing functions I would write:
>>
>> access()
>> {
>> lock.lockForRead();
>> device->doSomething();
>> lock.unlock();
>> }
>>
>
> Again, I would prefer to use QReadLocker.
>
I do not see the difference between QReadWriteLock and a combination of
QWriteLocker and QReadLocker,
especially because in your approach it would be two different lockers
which have nothing to do with each other?
>> But this would require to look at the status of the lock which seams not
>> possible?
>>
>
> How about the following function:
>
> http://doc.trolltech.com/4.6/qmutex.html#tryLock
>
> seems to do exactly what you need.
>
Yes, in case I am using a Mutex that should work.
>
>> 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?
>>
>
> If the timers are in the main thread they will not happen simultaneously with
> user interaction since they are both dispatched by the main thread's event
> loop and one thread cannot execute both handlers at the same time.
>
Yes, but both trigger an acces with a device which may have response
times in several 100 ms. So yes, in principle
it will never happen at the same time unless the actual device
communicating class itself is using some thread mechanism
which answers with a long delay.
Matthias
More information about the Qt-interest-old
mailing list