Results 1 to 4 of 4

Thread: thread - right approach?

  1. #1

    Default thread - right approach?

    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

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread - right approach?

    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.

  3. #3

    Default Re: thread - right approach?

    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

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: thread - right approach?

    QDir is not a widget. It is part of the core framework (non-gui).
    If you use it correctly, it will work.

Similar Threads

  1. Replies: 5
    Last Post: 17th January 2008, 21:49
  2. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  5. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.