[Qt-interest] Documentation on Mutex, Semaphore and Critical Section with Qt?
Sean Harmer
sean.harmer at maps-technology.com
Fri Jan 29 15:23:03 CET 2010
Hi,
On Friday 29 January 2010 14:09:53 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();
> }
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.
>
> 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?
How about the following function:
http://doc.trolltech.com/4.6/qmutex.html#tryLock
seems to do exactly what you need.
> 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.
Cheers,
Sean
More information about the Qt-interest-old
mailing list