QT3 -- How does a gui communicate with a worker thread?
QT3 -- How does a gui communicate with a worker thread?
I have a simple gui that creates and then launches a thread (derived from QThread). The thread does some continuous processing and remains for the life of the gui. In order for the thread to communicate with the gui, I use postevent -- which, as I understand, is the correct way for a thread to communicate with the gui.
However, I am not sure how to safely communicate from the gui to the thread. Right now, I am directly accessing member functions/variables of my derived thread class (since the gui created/launched it). This method isn't safe, right?
What is the "official" way for the gui to communicate with a thread? (Note: I am stuck using QT3)
Thank You!
Re: QT3 -- How does a gui communicate with a worker thread?
Quote:
Originally Posted by
Ronayn
QT3 -- How does a gui communicate with a worker thread?
Using events.
Quote:
I have a simple gui that creates and then launches a thread (derived from QThread). The thread does some continuous processing and remains for the life of the gui. In order for the thread to communicate with the gui, I use postevent -- which, as I understand, is the correct way for a thread to communicate with the gui.
That's correct.
Quote:
However, I am not sure how to safely communicate from the gui to the thread.
Basically, you can't. You can only use mutexes and a shared variable. Hard to call that "communication" though.
Re: QT3 -- How does a gui communicate with a worker thread?
Wysota, your first answer contradicts your third. To be crystal on this point, a QThread can use events to communicate (one-way) to the gui, but the gui cannot post events to the QThread (because it doesn't have an event loop). That is correct, yes?
So, you are saying QT3 doesnt provide a safe way for the gui to communicate to a QThread? If so, that's a bummer :(
Does QT4? (if you know)
Re: QT3 -- How does a gui communicate with a worker thread?
Quote:
Originally Posted by
Ronayn
Wysota, your first answer contradicts your third.
No, it doesn't :)
Quote:
To be crystal on this point, a QThread can use events to communicate (one-way) to the gui, but the gui cannot post events to the QThread (because it doesn't have an event loop). That is correct, yes?
Yes, only one-way communication is safe in Qt3.
Quote:
So, you are saying QT3 doesnt provide a safe way for the gui to communicate to a QThread? If so, that's a bummer :(
Not anything built-in. You can only use a mutex, have a queue of messages and periodically check the queue from within the thread.
Quote:
Does QT4? (if you know)
Yes, in Qt4 you can use signals and slots and events (both ways).