[Qt-interest] Documentation on Mutex, Semaphore and Critical Section with Qt?
Max Ringström
maxringstrom at gmail.com
Fri Jan 29 14:06:23 CET 2010
I'm not entirely sure if I've understood your problem correctly but a
semaphore in combination with a mutex seems to be what you need. A semaphore
is basically a mutex that allows more than 1 object to lock it.
My semaphore/mutex/condition/
monitor knowledge is a bit rusty but this is what I think, perhaps someone
can tell me if I'm correct.
There's one object that can be accessed from multiple threads.
When each thread is accessing the object a semaphore in that object is
increased. When the threads leaves the object the semaphore is decreased.
By using the semaphore you can know if there are any threads currently
accessing the object.
If you also use a mutex that states if the object can be entered you can
prevent access to the object until the member variable has been
reinitialized.
- mutex is unlocked
thread #1 waits on mutex and is allowed access, increases semaphore :
semaphore = 1
thread #2 waits on mutex and is allowed access, increase semaphore :
semaphore = 2
-- the object needs to reinitialize the member function -> waits for the
semaphore to reach 0
thread #1 decreases semaphore : semaphore = 1
thread #2 decreases semaphore : semaphore = 0
the object locks the mutex
thread #3 attempts to access object and waits on the mutex
the object checks that the semaphore is still 0 and reinitializes the member
variable,
the object unlocks the mutex
thread #3 can now access the object
I can't be sure about if this is correct when taking deadlock into account
and the exact usage of Qt's semaphores and mutexes, but perhaps it will help
you somewhat. As I mentioned, please correct me if I'm wrong.
/Max Ringström
On Fri, Jan 29, 2010 at 11:05 AM, Matthias Pospiech <
matthias.pospiech at gmx.de> wrote:
> Sean Harmer schrieb:
> > Hi,
> >
> > On Friday 29 January 2010 09:42:52 Matthias Pospiech wrote:
> >
> >> I am looking for general introduction and documentation of Mutex,
> >> Semaphore, Critical Section and such.
> >> (btw I could not see any difference between mutexes and critical
> >> sections from what I found on the web).
> >> Best would be if it would also describe the Qt-way of doing things if Qt
> >> offers something in that case.
> >>
> >>
> > Take a look at these and the links contained therein:
> >
> > http://doc.trolltech.com/4.6/threads.html
> > http://doc.trolltech.com/4.6/examples-threadandconcurrent.html
> >
> > The examples will give you a good feel for how to use the thread
> > synchronization classes in practice.
> >
> From all I can understand there is that QMutexLocker is a nice tool,
> but has nothing
> to do with locking several functions at once. It only works for a single
> function.
> And that is not my problem.
>
> 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
>
> and that applicationwide whereevery the class containing the member is
> used.
>
> I do not see where Mutex, Mutexlocker or Semaphores play any role in
> such a locking mechanism,
> especially if it is in different threads. Here I would love to see more
> examples.
> Previously I was sometimes told to use the wrong code design, but I need
> examples to design better.
>
> Matthias
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100129/74a060b1/attachment.html
More information about the Qt-interest-old
mailing list