PDA

View Full Version : Qt threads



^Nisok^
16th April 2009, 22:42
Hi to all!
I try to make my first multithreaded program. I use a linux system, but I don't think that has to do with anything. It happens that I don't have a past experience with threads. By now I didn't had to use but the main thread.

Can you pls direct me to the right direction. Maybe a tutorial or some guidelines.

Thank you for your time

Lykurg
17th April 2009, 07:35
A good point to start is always the documentation of Qt: Thread Support in Qt (http://doc.trolltech.com/4.5/threads.html). And then ervery book for Qt handles threads.

^Nisok^
17th April 2009, 11:05
ok. So as far as I understand I have to subclass my class from QThread. But what if I want to subclass it from another custom class.

^Nisok^
17th April 2009, 11:12
In order to be more specific...
I read the data from a list file, then I process the data to produce an image. But I want to have a periodical check or new data. My first thought was to create a new thread which will be responsible of doing this check and then provide the new data to the process class.

Lykurg
17th April 2009, 11:13
ok. So as far as I understand I have to subclass my class from QThread.
That's right,

But what if I want to subclass it from another custom class.
If the custom class is a subclass of QThread then that's also fine. Or you can subclass from QThread and the other class...

Lykurg
17th April 2009, 11:16
But I want to have a periodical check or new data.

If your check for new data is quick (I guess so) just use a timer inside your main app. If data chanced use/fire a worker thread .


My first thought was to create a new thread which will be responsible of doing this check and then provide the new data to the process class.

You also can use signal and slots across the thread border

^Nisok^
21st April 2009, 11:11
If I create a l class from a parent which runs on a separate thread then this class will also run on this thread?

spirit
21st April 2009, 11:13
all objects which have been created in QThread::run will be in this thread.

^Nisok^
21st April 2009, 20:00
Ok, thanks and one final question on this tread. On the worker thread I created I have a matrix which I want to transfer to a class on the main thread, is there something I have to take care of, I heard that in order to communicate a thread with the main thread I have to setup a transaction, do you know a guide or something?

^Nisok^
22nd April 2009, 14:27
anyone?
How to transfer data from a worker thread to the main thread?

spirit
22nd April 2009, 14:30
you need use QMutext/QMutexLocker, QReadWriteLock, QSemaphore, and QWaitCondition.

^Nisok^
22nd April 2009, 14:32
But this way will I lock the main thread during the data transfer??

spirit
22nd April 2009, 14:35
you need lock that thread which modify data and then you can send that data to another thread using signals or events.