views:

56

answers:

1

The simplified version of my code looks as follows:

class threadCreator
{
   void threadFunction(void){
   // use some private data members and do something.
   }
   void createThread(void){ 
      boost::thread myThread(
                boost::bind(&threadCreator::threadFunction,this));
      myThread.detach();
   }
}

This program waits for the thread to complete execution and then exits, even though I call a detach method on the thread. If I remove the class, and create my thread just in a function, this seems to work as expected.

I think it has something to do with the object still being in memory. However I am C++ learner and not sure on how to resolve this.

+1  A: 

I think you're missing something because for the program to wait for the end of the thread execution, you should have a .join() somewhere on this thread object. The default behavior is not to wait the end of the thread execution.

There is no need to call .detach() on the thread, the thread will be detached from the object thread at the end of the scope anyway.

Nikko