T-D
T-D

Reputation: 373

Signal from pthread to main

pthread_t thread_id;
while(1) {
   if(counter < 3) {
      // do something
      pthread_create( &thread_id , NULL , handle_me, (void*)arg);
   }
   else {
      // wait for counter to be changed 
      // pthreads will be changing the counter 
      // when changed, go back to beginning of loop 
      counter++;
   }
}

i am trying to achieve the following: signal from a pthread to main. what options do i have ?

counter is protected by a mutex when changed in threads.

Upvotes: 0

Views: 105

Answers (1)

abligh
abligh

Reputation: 25129

Use a condition variable. From the thread incrementing the counter use pthread_cond_signal or pthread_cond_broadcast. In the other thread that waits for the signal, use pthread_cond_wait or pthread_cond_timedwait.

Upvotes: 1

Related Questions