PDA

View Full Version : thread - right approach?



heinz32
17th June 2008, 00:32
Hi,

i need a thread to carry out certain tasks in a loop in a QThread.
However, i must be able to stop it from my main application, is creating a class variable like "bool stop", setting it false from main application and checking in my thread loop a proper way of doing this?

In my main application, i would call wait() after setting stop to false, and then create a new thread with different initial conditions. Can i use the same instance i used before or do i have to delete the old one and create a new one (ie just call start() again)?

What is a good way to pass initial parameters to the thread? Declare member variables and set them from the constructor?

Moreover, the thread will maybe access things from my main application, so i guess i should use a mutex. Is it right to declare the mutex in the class from where i call the new thread and then passing it to the new thread so both threads can lock/unlock it?

Lots of questions, i hope for lots of answers :) thank you in advance

marcel
17th June 2008, 00:47
However, i must be able to stop it from my main application, is creating a class variable like "bool stop", setting it false from main application and checking in my thread loop a proper way of doing this?
Yes, this is a good way. Just make sure you declare the variable as volatile.


In my main application, i would call wait() after setting stop to false, and then create a new thread with different initial conditions. Can i use the same instance i used before or do i have to delete the old one and create a new one (ie just call start() again)?
It is better to connect a slot to the thread's finished() signal and wait for it to terminate, and then start a new, clean instance.


What is a good way to pass initial parameters to the thread? Declare member variables and set them from the constructor?
That's ok... Just it depends on what objects you pass to the thread. Make sure you don't pass pointers to objects that will later be used in the main thread or any other thread(unless you synchronize access to those objects).



Moreover, the thread will maybe access things from my main application, so i guess i should use a mutex. Is it right to declare the mutex in the class from where i call the new thread and then passing it to the new thread so both threads can lock/unlock it?

If you're going to access a main thread resource from a worker thread then I don;t see the point in having the mutex in both threads. The mutex should be in the thread that owns the resource.

heinz32
17th June 2008, 16:52
ok thanks, i now created my thread class

the problem is: i need a QDir in this thread to browse folders, but Qt says
"Widgets must be created in the GUI thread." and quits my application when creating. What should i do now? Create it in the gui thread and pass the pointer? Searching dirs recursively could become tricky when im not allowed to create a Qdir instance in my thread

marcel
17th June 2008, 17:39
QDir is not a widget. It is part of the core framework (non-gui).
If you use it correctly, it will work.