Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:32:11+00:00 2026-05-23T07:32:11+00:00

I am doing a project on VoIP and I have got pthreads in my

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:32:12+00:00Added an answer on May 23, 2026 at 7:32 am

    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 and recv 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently am doing project on Family Business. In that I need some video calling
I am doing a project related to payroll where i will have some payperiodnumbers
I am doing a project in which i have to do some updations over
I am doing project euler question 33 and have divised a refactor to solve
Im doing this project where i need to download files through a webservice (images,
I've been doing a project at work recently focused on an almost entirely client-driven
I'm doing a project in seam that requires restful URLs. I have a view
I'm doing project in asp.net mvc with Micorsoft Access database and I need to
while doing flex project iam unable to start my Jboss server. can any one
I am doing a project that has dependencies on some classes from the mahout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.