Click to See Complete Forum and Search --> : To thread or not to thread?
lilsim89
May 23rd, 2009, 05:51 AM
Any advice appreciated:
I'm writing a C++ (Qt for GUI) application that needs to run a function about every minute, though the GUI should still be responsive throughout.
Simply put, do I need to multithread, or is there some other method that I should look into?
Alsvha
May 23rd, 2009, 01:19 PM
I would thread it. It means you can update the UI - keeping it responsive - without having to wait for process intensive functions to run first.
JVene
May 23rd, 2009, 01:47 PM
Definitely thread it.
You'll want to create a thread that waits on a signal (consider an autoreset event). You'll signal through the event, which releases the thread to do it's work, then it will wait again.
You can use a timer in the GUI thread to initiate a message that signals the event. You may be tempted to use sleep in the thread instead, and while that's also viable, the signal approach is more widely useful for a wide range of purposes, including a queue of jobs. Using bind from boost or some other wrapper for a functor or pointer to function, the queue can be generalized to service any threaded operation. As a design paradigm, I find it indispensable. If you do it right, it can become a small library applicable to all future threading situations.
lilsim89
May 23rd, 2009, 06:51 PM
Thanks you guys, I guess I'm going to find a good threading tutorial and take it from there.
- Simon
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.