Question (pthread): How to Signal all threads w/o "broadcast"?

Hello all,

Is there any way that I can signal (wake) all threads that I have created without using pthread_cond_broadcast?

Cheers!
Aaron

You can either:

  • wake all the threads waiting on the condvar with pthread_cond_broadcast,
  • or wake one of the thread waiting on the condvar with pthread_cond_signal (which one is left to the libpthread/OS).

What are you trying to achieve exactly?

Cheers, Lo�c

I am trying to achieve this wake up without using pthread calls, such as broadcast.
Basically am trying to find a solution with - for example - using semaphores.

Cheers,
Aaron

This should roughly do the job:

// postsem and waitsem should be created with values of 0

// to wake up n threads
sem_post(waitsem);
for(i=0; i<n; n++) sem_wait(postsem);

// to wait until woken
sem_wait(waitsem);
sem_post(waitsem);
sem_post(postsem);

Not the most efficient, since it needs 4n+1 semaphore operations to deal with n threads though... And it has race conditions where threads might not sleep if they try to wait when other threads are in the process of being woken up.

Beyond that I'm not sure there's a portable way to do what you want. This is what pthreads is supposed to be for.

Why not using what is offered by Pthreads? Is it a homework, or something like that?

Lo�c

Hello all,

Thank you - apparently there is no efficient way to do this other than pthreads I guess - my question was due to curiosity.

@Loic: Well, I am 30 and I wish I could go back to those "homework" days considering how I live today : )

Cheers,
Aaron

Hi Aaron,

I can understand that :slight_smile:

Lo�c