OSF DCE Application Development Guide--Core Components

OSF DCE Application Development Guide—Core Components
protocol needs to be performed on all global data to ensure its visibility and protect its
integrity.
Each worker thread then determines if its current work item (a number) is prime by
trying to divide numbers into it. If the number proves to be nondivisible, it is put on the
list of primes. Cancels are explicitly turned off while working with the list of primes in
order to better control any cancels that do occur. The list and its current count are
protected by locks, which also protect the cancellation process of all other worker
threads upon finding the last requested prime. While still under the prime list lock, the
current worker checks to see if it has found the last requested prime, and if so unsets a
predicate and cancels all other worker threads. Cancels are then reenabled. The
canceling thread falls out of the work loop as a result of the predicate that it unsets.
The parent thread’s flow of execution is as follows: set up the environment, create
worker threads, broadcast to them that they can start, join each thread as it finishes, and
sort and print the list of primes.
Setting up of the environment requires initializing mutexes and the one condition
variable used in the example.
Creation of worker threads is straightforward and utilizes the default attributes
(pthread_attr_default). Note again that locking is performed around the predicate
on which the condition variable wait loops. In this case, the locking is simply done
for visibility and is not related to the broadcast function.
As the parent joins each of the returning worker threads, it receives an exit value
from them that indicates whether a thread exited normally or not. In this case the exit
values on all but one of the worker threads are -1 , indicating that they were canceled.
The list is then sorted to ensure that the prime numbers are in order from lowest to
highest.
The following pthread routines are used in this example:
pthread_cancel( )
pthread_cond_broadcast( )
pthread_cond_init()
pthread_cond_wait( )
pthread_create()
pthread_detach()
pthread_exit( )
pthread_join( )
pthread_mutex_init( )
pthread_mutex_lock( )
pthread_mutex_unlock( )
pthread_setcancel( )
pthread_testcancel( )
10 2 Tandem Computers Incorporated 124245