Results 1 to 3 of 3

Thread: How can I implent an thread manager?

  1. #1
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    2
    Platforms
    Windows

    Default How can I implent an thread manager?

    I do something in MyThread, the main run() is infinite loop until stop() is called(canRun change to false)
    ThreadManager is a class which manger threads. When sometime calls Delete(MyThread * thr), "thr" will be pushed in a queue, and the main run() of ThreadManager get a MyThread pointer from the queue and delete the thread.

    I want to block the thread of ThreadManager until "thr" can be deleted, my problem is I don't know when "thr" is terminated.

    This is my code here.
    Thanks for any suggestions.
    Qt Code:
    1. class MyThread : public QThread
    2. {
    3. public:
    4. MyThread() { canRun = true; }
    5.  
    6. void run()
    7. {
    8. while(canRun)
    9. {
    10. msleep(50);
    11. /* do something */
    12. }
    13. exec();
    14. }
    15. void stop() { canRun = false; }
    16. private:
    17. bool canRun;
    18. };
    19.  
    20.  
    21. class ThreadManager : public QThread
    22. {
    23. public:
    24. void run()
    25. {
    26. MyThread * thr;
    27. queue.clear();
    28. while(true)
    29. {
    30. msleep(50);
    31. while(!queue.isEmpty())
    32. {
    33. thr = queue.dequeue();
    34. thr->stop();
    35. /* I want to block this thread until
    36.   * "thr" terminated(can be deleted) */
    37. delete thr;
    38. }
    39. }
    40.  
    41. }
    42.  
    43. MyThread * New() { return (new MyThread); }
    44. void Delete(MyThread * thr) { queue.push_back(thr); }
    45.  
    46. private:
    47. QQueue<MyThread*> queue;
    48. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I implent an thread manager?

    The following line should do (using QThread::wait):

    Qt Code:
    1. thr->wait();
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to shentian for this useful post:

    howie1013 (19th June 2009)

  4. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can I implent an thread manager?

    You should maybe also have a look at the QThreadPool class and the Mandelbrot example that comes with Qt.

  5. The following user says thank you to shentian for this useful post:

    howie1013 (19th June 2009)

Similar Threads

  1. Thread Ownership Problem
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 9th June 2009, 00:18
  2. Thread Help
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 22:10
  3. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

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.