pthreads - Stop the current execution of threads and restart them after a specific event
I am doing a project on VoIP and I have got pthreads in my C code. I need to start the pthreads and make them work with some sleep in between them. Right now my threads are running and when I get a session end from the server I need to stop the running threads and restart them again new from the beginning.
My code looks something like this:
void *recv_thre开发者_StackOverflow社区ad(void *arg)
{
/*receive the multimedia data and close the recv_thread when there is no more data to receive*/
}
void *send_thread(void *arg)
{
/*send the multimedia data*/
}
send_recv_data()
{
pthread_create(thread2, NULL, send_thread, NULL);
pthread_create(thread3, NULL, recv_thread, NULL);
}
void *first_thread(void *arg)
{
/*if some condition met the start the routine for creation of two threads one for receiving and one for sending data*/
if(cond == TRUE){
send_recv_data();
}
}
main()
{
pthread_create(thread1, NULL, first_thread, NULL);
}
My question is that once I receive a message from the other user agent that it is sending me no more data then I need to stop both the send and recv threads and then finally first_thread which is responsible for the creation of the other two threads. Once i stop all the threads I need to restart them again. I tried using mutexes and conditional variables but all went in vain.
Any idea of how I can overcome this, may be a small piece of simple code would be much more helpful
Thanks
Why do you need to start and stop the threads? It is usually better to keep threads running, and have them block on something (such as a condition variable) when they have no useful work to do.
I'm going to make a few assumptions because, as more knowledgable people than me have pointed out, this kind of behavior immediately raises the question of why you (think you) need to destroy/recreate these threads, and why this is as obfuscated as it is:
- You have a valid reason for needing to destroy/recreate the threads
- You have a valid reason for adding this many possibly useless layers to the thread creation process
- Your code which will "receive a message from the other user agent" has access to the thread ids
- Your
send
andrecv
functions can have access to some sort of flagging mechanism
This is such a shoe-horn approach that I'm almost afraid to approach it. Without knowing more about the constraints of your design, it's difficult to express more than some of the options you can start exploring.
First, let's set up the send
and recv
functions so that they can be notified that it's time to go bye-bye:
void* send_thread(void *arg)
{
pthread_mutex_lock(&wrapUpFlagMutex);
bool timeToQuit = wrapUpFlag;
pthread_mutex_unlock(&wrapUpFlagMutex);
while( timeToQuit == false )
{
...
// You're doing something here
...
pthread_mutex_lock(&wrapUpFlagMutex);
timeToQuit = wrapUpFlag;
pthread_mutex_unlock(&wrapUpFlagMutex);
}
// We've been flagged! Get out...the join will catch us.
pthread_exit();
}
Now change the code which somehow knows when the magic needs to end and be restarted:
dontShootTheMessenger()
{
...
// We've just determined that those threads need to be restarted
// Flag those functions to wrap it up
pthread_mutex_lock(&wrapUpFlagMutex);
wrapUpFlag = true;
pthread_mutex_unlock(&wrapUpFlagMutex);
// Join the threads, note that this will block
pthread_join(thread3, NULL);
pthread_join(thread2, NULL);
pthread_join(thread1, NULL);
// Flag those functions to...not...wrap it up
pthread_mutex_lock(&wrapUpFlagMutex);
wrapUpFlag = false;
pthread_mutex_unlock(&wrapUpFlagMutex);
// Launch those puppies again
pthread_create(thread1, NULL, first_thread, NULL);
...
}
Again, this is a mimimalist approach. A more robust method will probably involve conditional variables, a redesign of your calling structure, actually using the passed arguments to the thread functions and return values for pthread_exit()
, and much more.
Also, depending on your contraints, you may be interested in functionality like pthread_kill. Note that whatever road you go down, you're not saving yourself any trouble by just hoping that the act of killing off threads somehow cleans things up for you.
精彩评论