This threaded code can be improved further: =============== ~WorkerThread()
{
while (!m_Running)
;
m_Thread.join();
m_Running = false;
}
================
Endlessly polling the m_Running with an empty while loop is inefficient. It would be better to use the boost::condition and boost::mutex for thread synchronization [the 2nd thread notifies the 1st (main() ) one when it completes its task]. A possible improvement can be achieved like this:
--------------Begin------------- boost::condition taskCompleted; boost::mutex taskMutex;
class WorkerThread { // class definition ... ~WorkerThread() { boost::mutex::scoped_lock lock(taskMutex);
{ taskCompleted.wait(lock); // wait for notification m_Thread.join(); } ... void run() { boost::mutex::scoped_lock lock(taskMutex);
{ // work to do // after work is done taskCompleted.notify_one();// I am done, your turn now. } // lock scope } run() scope }; // class definition
--------------end------------- The volatile bool m_Running variable, its initialization and assignments, and const bool isRunning() const function in the original code are no longer needed.