Update GUI from another object with 2 threads.
-
Hi, all:
I've got a class which has a producer thread and a consumer thread. I would like the producer thread keeps producing the image data, and whenever the consumer thread is consuming the image data from the shared buffer, a signal will be sent out to the MainWindow GUI, for the purpose of redraw the image on GUI --- this signal/slot is of Qt::BlockingQueuedConnection for sure.
However, I've got no idea why this doesn't work for me now (no image has ever been drawn on the MainWindow GUI). I guess it's probably because, the signal sent out to the MainWindow GUI is embedded inside the critical section -- the consumer thread lock the critical shared buffer using a mutex, and I sent out the signal from within this locked mutex section to the MainWindow GUI. Is this problematic? And is there a way out for my issue?
Cheers
Pei -
I know attached my code at "http://visionopen.com/questions/QtThreadExample.tar.gz":http://visionopen.com/questions/QtThreadExample.tar.gz . Can anybody help to explain what's wrong with my code? I do need your help please
Cheers
Pei -
I'm not sure if this is part of your issue, but you're pushing and popping the same value from your buffer over and over again: m_iIdx never changes.
Anyway, I notice you're using Boost threads with lots of sleep() and wait(). I'm not very familiar with this approach so I can't debug your thread logic, sorry.
How big are your items, and how fast do you produce/consume them? If the items are small (or if they get consumed slowly), I suggest trying event-driven programming with Qt:
Every time a consumer consumes an item, emit a consumed() signal
When the producer receives the consumed() signal, produce a new item. Emit the item using a produced(item) signal.
When the consumer receives the produced() signal, consume the item (go back to #1)
This method is cleaner, you don't have to worry about deadlocks, and you can even implement the producer-consumer pattern using only 1 thread.
-
Hi, thanks JKSH.
I solved that problem by removing the join().
Consumer join() and producer join() blocks the main thread, namely MainWindow GUI.Thanks anyway.