[clug] pthread coroutines

Andrew Over andrew at cs.anu.edu.au
Wed Jul 2 22:00:41 EST 2003


I replied to this earlier, but I think AARNET's DNS issues may have
eaten it (either that or it's taking a while to retry).

On Wed, Jul 02, 2003 at 07:44:18PM +1000, Jepri wrote:

> Unless you use TWO mutexes, for instance.  Or two semaphores, just like 
> in almost every example of threading I've read so far...
> 
> Just have each thread wait on a different semaphore.  Have each routine 
> lift the others semaphore, and lowier it's own.

Condition variable and a mutex.  Every time you want to change the
active thread (ie you're done):

  pthread_mutex_lock(&mtx);
  pthread_cond_signal(&cvar);
  pthread_cond_wait(&cvar, &mtx);
  pthread_mutex_unlock(&mtx);

The mutex is only necessary to avoid a race condition between a thread
trying to wait and a thread trying to signal.

Cheers,
--Andrew



More information about the linux mailing list