Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 7

Qt-interest Archive, March 2007
synchronization


Message 1 in thread

Hi,

I have an app with a giu and a worker thread. The worker is supposed to
run all the time, but wyit when there is nothing to do. I want to change
to this behaviour from spawning a new worker for every cycle of work
because it would be more debuggable and because I think I have some race
conditions right now because I cannot exactly determine when a thread
ends so that there can be overlap between threads.

Now I could not find a wait/notify mechanism as in Java, but only the
QMutex/QSemaphore. The way I came up with is this

- one mutex exists for the worker thread which is initially locked
- the worker thread locks the mutex at the beginning of each cycle
- to process one cycle the gui thread unlocks the lock, so the worker
  can acquire it and process a cycle. the worker does not unlock the
  lock, so at the beginning of the next cycle it will wait again (this
  can be generalized to n cycles by having the worker unlock the mutex
  n-1 times)


This seems to work, but in debug build on every cycle qt complains that
it found a deadlock. now I suppose that is because qt considers my way
of making the worker wait a deadlock (which it is, but a controlled one...).
Is there anything wrong with my approach, or is qt just confused and all
is dandy?

Daniel

--
 [ signature omitted ] 

Message 2 in thread

On Tuesday 27 March 2007 16:00, Daniel Oberhoff wrote:
> Is there anything wrong with my approach, or is qt just confused and all is
> dandy? 

If you are using QMutex, then your approach is wrong, see 
http://doc.trolltech.com/4.2/qmutex.html#unlock, specifically:

"Attempting to unlock a mutex in a different thread to the one that locked it 
results in an error."

You want to use QSemaphore instead and call acquire() in the worker thread, 
and release() in the gui thread.

-- 
 [ signature omitted ] 

Message 3 in thread

On Wednesday 28 March 2007 01:19, Daniel Oberhoff wrote:
> On Mar 27, 2007, at 4:40 PM, Bradley T Hughes wrote:
> > On Tuesday 27 March 2007 16:00, Daniel Oberhoff wrote:
> > > Is there anything wrong with my approach, or is qt just confused
> > > and all is
> > > dandy?
> >
> > You want to use QSemaphore instead and call acquire() in the worker
> > thread,
> > and release() in the gui thread.
>
> Ah, ok. Thanks. I had stopped reading once I had come across "the
> semaphore is basically an extended mutex with a counter" or similar
> in the docs.

Not a problem ;)

-- 
 [ signature omitted ]