CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Posts
    19

    Stop and Start pthreads

    I have got pthreads in m C code. I am doing a project on VoIP and I need to start the pthreads and make them work with some milliseconds sleep time. 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:

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Stop and Start pthreads

    Why do you need to stop the threads and then start them again?
    It's usually a better design to have your threads run a loop to process tasks that are assigned to it. Tasks are typically some kind of function object and each thread typically has a queue of tasks that need to be processed (or you can share a queue between several threads). When there is nothing to be processed (i.e. queue is emtpy) the thread just waits until something is pushed onto the queue.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured