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_thread(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