OSF DCE Application Development Guide--Core Components
DCE Threads Example
10.2 DCE Threads Example Body
The following is the DCE Threads example:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
/*
* Constants used by the example.
*/
#define workers 5 /* Threads to perform prime check */
#define request 110 /* Number of primes to find */
/*
* Macros
*/
#define check(status,string) if (status == -1) perror (string)
/*
* Global data
*/
pthread_mutex_t prime_list; /* Mutex for use in accessing the prime */
pthread_mutex_t current_mutex; /* Mutex associated with current number */
pthread_mutex_t cond_mutex; /* Mutex used for ensuring CV integrity */
pthread_cond_t cond_var; /* Condition variable for thread start */
int current_num= -1;/* Next number to be checked, start odd */
int thread_hold= 1; /* Number associated w/condition state */
int count=0; /* Prime numbers count;/index to primes */
int primes[request];/* Store primes; synchronize access */
pthread_t threads[workers]; /* Array of worker threads */
/*
* Worker thread routine.
*
* Worker threads start with this routine, which begins with a condition
* wait designed to synchronize the workers and the parent. Each worker
* thread then takes a turn taking a number for which it will determine
* whether or not it is prime.
*
*/
void
prime_search (pthread_addr_t arg)
{
div_t div_results; /* DIV results: quot and rem */
int numerator; /* Used for determing primeness */
int denominator; /* Used for determing primeness */
int cut_off; /* Number being checked div 2 */
int notifiee; /* Used during a cancellation */
int prime; /* Flag used to indicate primeness */
int my_number; /* Worker thread identifier */
int status; /* Hold status from pthread calls */
int not_done=1; /* Work loop predicate */
my_number = (int)arg;
/*
* Synchronize threads and the parent using a condition variable,
124245 Tandem Computers Incorporated 10−3