OSF DCE Application Development Guide--Core Components
OSF DCE Application Development Guide—Core Components
* for which the predicate (thread_hold) will be set by the parent.
*/
status = pthread_mutex_lock (&cond_mutex);
check(status,"1:Mutex_lock bad status\n");
while (thread_hold) {
status = pthread_cond_wait (&cond_var, &cond_mutex);
check(status,"3:Cond_wait bad status\n");
}
status = pthread_mutex_unlock (&cond_mutex);
check(status,"4:Mutex_unlock bad status\n");
/*
* Perform checks on ever larger integers until the requested
* number of primes is found.
*/
while (not_done) {
/* cancellation point */
pthread_testcancel ();
/* Get next integer to be checked */
status = pthread_mutex_lock (¤t_mutex);
check(status,"6:Mutex_lock bad status\n");
current_num = current_num + 2; /* Skip even numbers */
numerator = current_num;
status = pthread_mutex_unlock (¤t_mutex);
check(status,"9:Mutex_unlock bad status\n");
/* Only need to divide in half of number to verify not prime */
cut_off = numerator/2 + 1;
prime = 1;
/* Check for prime; exit if something evenly divides */
for (denominator = 2; ((denominator < cut_off) && (prime));
denominator++) {
prime = numerator % denominator;
}
if (prime != 0) {
/* Explicitly turn off all cancels */
pthread_setcancel(CANCEL_OFF);
/*
* Lock a mutex and add this prime number to the list. Also,
* if this fulfills the request, cancel all other threads.
*/
status = pthread_mutex_lock (&prime_list);
check(status,"10:Mutex_lock bad status\n");
if (count < request) {
primes[count] = numerator;
count++;
10 − 4 Tandem Computers Incorporated 124245