Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pthread_cond_signal() blocks?

8 views
Skip to first unread message

The Magician

unread,
Jan 23, 2002, 5:49:53 AM1/23/02
to
Hi,


Imagine the scenario where i have 3 threads, one main thread that
waits that the other two finish their tasks.
i use pthread_cond_signal() on the worker threads to signal the main
thread that the task is finished.
My question is, if i spawn the two threads before the main thread
reaches the pthread_cond_wait() one of the threads may reach
pthread_cond_signal() before i have the pthread_cond_wait() blocking
and waiting for signaling, that is:

Time: Execution:

0 Thread1 - Launched
1 Thread1 - executes pthread_cond_signal()
2 Thread2 - Launched
3 Main thread - executes pthread_cond_wait()

In this scenario the signal from Thread1, is lost? what happens?
pthread_cond_signal() blocks until i have a pthread_cond_wait(), or
simply continues execution?

One soluttion is acquire the mutex in the main thread before i launch
the threads, but it is a good programming technique?

Any comments, would be appreciated.

Thanks,
The Magician

crux

unread,
Jan 23, 2002, 7:35:26 AM1/23/02
to
On Wed, 23 Jan 2002 11:49:53 +0100, The Magician wrote:

> Hi,
>
>
> Imagine the scenario where i have 3 threads, one main thread that waits
> that the other two finish their tasks. i use pthread_cond_signal() on
> the worker threads to signal the main thread that the task is finished.
> My question is, if i spawn the two threads before the main thread
> reaches the pthread_cond_wait() one of the threads may reach
> pthread_cond_signal() before i have the pthread_cond_wait() blocking and
> waiting for signaling, that is:
>
> Time: Execution:
> 0 Thread1 - Launched
> 1 Thread1 - executes pthread_cond_signal() 2 Thread2 -
> Launched
> 3 Main thread - executes pthread_cond_wait()
>
> In this scenario the signal from Thread1, is lost? what happens?
> pthread_cond_signal() blocks until i have a pthread_cond_wait(), or
> simply continues execution?

At very least, you shoul read the prototype,
pthread_cond_wait(cond,mutex) automatically release the mutex, you have
to lock it before

Also, you can use kill: block signal in main thread, install othre
threads which kill main thread at the end

you can also use semaphores


>
> One soluttion is acquire the mutex in the main thread before i launch
> the threads, but it is a good programming technique?

mh, a mutex and a condition variable is another solution

Alexander Terekhov

unread,
Jan 23, 2002, 9:01:08 AM1/23/02
to
carlos...@oni.pt (The Magician) wrote in message news:<65e0b205.02012...@posting.google.com>...

> Hi,
>
>
> Imagine the scenario where i have 3 threads, one main thread that
> waits that the other two finish their tasks.
> i use pthread_cond_signal() on the worker threads to signal the main
> thread that the task is finished.
> My question is, if i spawn the two threads before the main thread
> reaches the pthread_cond_wait() one of the threads may reach
> pthread_cond_signal() before i have the pthread_cond_wait() blocking
> and waiting for signaling, that is:

Perhaps you are just mixing up state-less
condition variables with some other state-full
(and therefore rather poor/slow/error-prone)
signaling mechanism(s) - for example, semas
or MS-events, etc. Think of condition variables
this way:

http://groups.google.com/groups?as_umsgid=3A6CA6A5.66DE2264%40webmaster.com

"In fact, you could implement pthread_cond_wait
by releasing the mutex, sleeping a random amount
of time, and then reacquiring the mutex. Of course,
this would be a pretty poor implementation, but any
code that didn't work under that implementation
wouldn't be strictly compliant. DS"

BTW, David, thanks again for this bit!

regards,
alexander.

0 new messages