T.R | Title | User | Personal Name | Date | Lines |
---|
1538.1 | use a counter? | FLOYD::YODER | It's 999,943 to 1 but it just might work | Mon May 05 1997 13:32 | 24 |
| How about using a counter, n_signals, of the number of times 'pulse' has been
called? (It must be made large enough to not overflow: 128 bits ought to always
suffice, for example.) Then the implementation would be something like:
class event {
pthread_cond_t cond;
pthread_mutex_t mutex;
long n_signals = 0; /* Can be made a pair of longs */
void signal() {
long old_n_signals;
pthread_mutex_lock(&mutex);
old_n_signals = n_signals;
while (n_signals == old_n_signals) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
}
void pulse() {
pthread_mutex_lock(&mutex);
n_signals++;
pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);
}
};
|
1538.2 | Mike's idea is fine; but be sure to use a mutex! | WTFN::SCALES | Despair is appropriate and inevitable. | Mon May 05 1997 15:43 | 24 |
| Re .0: When you set the predicate and/or broadcast the condition variable you
absolutely MUST do so under a mutex. Otherwise, your code may not function
reliably.
Re .1, Mike's idea looks fine to me. (The alternative is to maintain a list of
waiters, have each waiter use a separate predicate, have the waker set each
predicate, and then wake each waiter either by broadcasting on a common
condition variable or by signalling each thread waiting on its own CV...)
.1> It must be made large enough to not overflow
I don't see any reason why this would be true. The code works just fine if the
counter wraps, just so long as the counter doesn't manage to wrap all the way
around to the previous value at the moment the waiter manages to wake up and
check (and, with at least 16 bits I'd say you're pretty safe, the more the safer
obviously).
.1> 128 bits ought to always suffice, for example.
"Always"...until the processors get "really fast"... ;-)
Webb
|
1538.3 | Add a cancellation handler. | WTFN::SCALES | Despair is appropriate and inevitable. | Mon May 05 1997 15:45 | 7 |
| Oh, and one other thing: condition waits are cancellation points, so if you
want your code to be "cancel-safe" (and you should want this, if you're
providing an API), you'll need to use an exception handler or a cancellation
handler to unlock the mutex in the event of a cancellation.
Webb
|