[clug] pthread coroutines
jepri at webone.com.au
Wed Jul 2 19:44:18 EST 2003
On 2003.07.02 19:33, Eyal Lebedinsky wrote:
> Simon Burton wrote:
> > Hi,
> > How to use mutexes so that effectively only one out of two threads
> > is running at a time? Like co-routines. Each thread blocks on a
> > pthread_mutex_lock() while the other is running. I'm sure it will
> come to me
> > eventually. Or is there Another Way?
> Yes, you use mutexes (or some other related synchronization verbs).
> However, there is no telling who will run after you release a lock
> and then aquire it again. You may get it or the other process may
> get it first. In other words, fairness is not guaranteed.
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.
More information about the linux