creating pthreads runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neel

    creating pthreads runtime

    Hi,
    I 'm facing some prob with creating pthreads.
    My requirement is to create threads in a loop.
    but I dont want to make an array of pthread.

    At anytime I want to have 5 threads (I used a global variable as a
    counter).
    Whenever the counter is less than 5, I want to create new thread.

    whenever any thread is about to terminate, I will decrement the
    counter. (of course avoiding race conditions using mutex lock).

    But 'm confused how to create thread at run time.

    I tried something like


    pthread_mutex_l ock(&check_thre ad_count);

    if(threadCounte r<5){
    pthread_create( new pthread(), NULL, handleConnectio n, NULL);
    }

    pthread_mutex_u nlock(&check_th read_count);

    using *new pthread()* is not correct.

    So how can I create pthread at run time?
  • Neel

    #2
    Re: creating pthreads runtime

    sorry friends,
    I made syntax error there...
    I got the solution..
    It should be

    new pthread_t()

    i missed "_t"

    Happy Programming :)

    Comment

    • CBFalconer

      #3
      Re: creating pthreads runtime

      Neel wrote:
      >
      I'm facing some prob with creating pthreads.
      My requirement is to create threads in a loop.
      but I dont want to make an array of pthread.
      >
      .... snip ...
      >
      using *new pthread()* is not correct.
      >
      So how can I create pthread at run time?
      There are no pthreads in the C language. Thus this is off-topic.

      --
      [mail]: Chuck F (cbfalconer at maineline dot net)
      [page]: <http://cbfalconer.home .att.net>
      Try the download section.

      Comment

      • Keith Thompson

        #4
        Re: creating pthreads runtime

        Neel <a.k.vora@gmail .comwrites:
        I 'm facing some prob with creating pthreads.
        [...]

        Try asking in comp.programmin g.threads.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Antoninus Twink

          #5
          Re: creating pthreads runtime

          On 23 Sep 2008 at 2:32, Neel wrote:
          On On 23 Sep 2008 at 2:02, Neel wrote:
          if(threadCounte r<5){
          pthread_create( new pthread(), NULL, handleConnectio n, NULL);
          }
          >
          I got the solution..
          It should be
          >
          new pthread_t()
          >
          i missed "_t"
          This is a recipe for a memory leak. As you never keep track of the
          pthread_t * you get from new (an operator which incidentally is only
          available in C++, not in vanilla C), you'll have no way of freeing the
          memory with delete once the thread exits.

          You should either have a pthread_t variable and pass its address to
          pthread_create( ), or else keep a record of the pointer from new,
          use pthread_join() somewhere appropriate and make sure to delete/free()
          the memory used by the pthread_t structure.

          Comment

          Working...