Nick
Nick

Reputation: 461

Basic question in creating thread in C++

I have a class member functionA which creates a thread to run another functionB. FunctionB would perform some operation and stop after some time ( it is kind of fire and forget function call and the number of functionA calls and threads needed would depends on the run time result). FunctionA would be called multiple times. I realized that the pthread_create would take in pthread_t for the first parameter, and the pthread_t must be available while creating the thread. So, I could not declare it as local like below. So where could I declare the pthread_t?

void classA::functionA(int param)
{ 
   pthread_t t; //could not declare local because functionA might return and destroy t before thread being created.
   pthread_create(&t , NULL, functionB, param);
}

void functionB(int param)
{

}

Upvotes: 1

Views: 857

Answers (4)

Anthony Williams
Anthony Williams

Reputation: 68701

Firstly, you can put your pthread_t on the stack: your functionA is fine in that respect. pthread_create stores the thread handle directly, so as soon as it returns the handle is valid. The newly created thread hasn't necessarily started executing yet, but that doesn't matter, as it won't touch that handle.

However, you need to manage your thread somehow. If you don't explicitly create it detached, then you must call pthread_join or pthread_detach on your handle. If you don't call either then you are leaking resources. If this really is a "fire and forget" thread then you should use pthread_detach to ensure that the OS cleans up when the thread finishes.

Either way, you need to ensure that the objects accessed by the thread outlive any potential accesses to them by the thread (e.g. by not destroying them until an "I don't need these objects any more" flag is set)

Upvotes: 2

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136515

You can have a pthread_t variable on the stack, there is no problem with that.

Is there a reason you don't use a thread wrapper like boost::thread? It would make your life much easier. http://www.boost.org/doc/libs/1_45_0/doc/html/thread/thread_management.html

Another thing is that you can't just fire and forget a thread because once you do the object must not be destroyed before the other thread stops accessing it. In other words, the destructor of your object must make sure there are no other threads accessing the object.

Upvotes: 1

Viren
Viren

Reputation: 2171

I would do it this way:

class classA
{
    void functionA();
    static void void functionB(classA* aThis);
};

void classA::functionA(int param)
{ 
//create thread: note that this way pthread_t can even become the class-member 
}

void classA::functionB(classA* aThis)
{
    classA* theThis = static_cast<classA*>(aThis);
    //now you can access classA members here easily
}

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122458

I don't believe you have a problem as pthread_create() won't return before the new thread identifier has been written into t so there is no need to do new pthread_t at all. You need to check the return from pthread_create() of course though.

Upvotes: 0

Related Questions