pthread_attr_t tattr; /*thread attributes/*
pthread_t tid;
/*thread descriptors*/
pthread_attr_init(&tattr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_create(&tid, &tattr, start_func,
arg);
pthread_exit(value);
pthread_join(tid, value_ptr);
# include <pthreads.h>
# include <semaphore.h>
# define SHARED 1
# include <stdio.h>
void *Producer(void*); /* the two threads */
void *Consumer(void*);
sem_t empty, full; /* global semaphores*/
int data;
/* shared buffer */
int numIters;
/* main() -- read command line and create threads */
int main(int argc, char *argv[] ) {
pthread_t pid, cid;
/* thread and attributes*/
pthread_attr_t attr;
/* descriptors
*/
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
sema_init(&empty, SHARED, 1); /*sem empty
= 1*/
sema_init(&full, SHARED, 0);
/* sem full = 0 */
numIters = atoi(argv[1]);
pthread_create(&pid, &attr, Producer,
NULL);
pthread_create(&cid, &attr, Consumer,
NULL);
pthread_join(pid,NULL);
pthread_join(cid,NULL);
}
/* depost 1, ... numIters into the data buffer */
void *Producer(void *arg) {
in t produced;
for (produced = 1; produced <= numIters; produced++)
{
sem_wait(&empty);
data = produced;
sem_post(&full);
}
}
/* fetch numIters items from the buffer and sum them
*/
void *Consumer(void *arg)
{
int total = 0, consumed;
for ( consumed =1; consumed <= numIters; consumed++)
{
sem_wait(&full);
total = total + data;
sem_post(&empty);
}
printf("the total is %d\n", total);
}
pthread_mutex_t mutex;
....
pthread_mutex_init(&mutex,
NULL);
pthread_mutex_lock(&mutex);
critical section;
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&mutex);
followed by
pthread_cond_wait(&cond, &mutex);
causes the thread to release the mutex and wait on cond. When the thread is "signal"ed it will then again acquire the lock.
pthread_cond_signal(&cond);
issued by another thread will
awaken it; once the signaling process gives up the lock, a thread waiting
on the condition will again acquire the lock and continue executing mutually
exclusively.
pthread_mutex_t barrier; /*lock
for the barrier*/
pthread_cond_t go ;
/*condition variable
*/
int numWorkers;
/*number of worker threads
*/
int numArrived = 0;
/*number who have arrived */
/* a
reusable counter barrier */
void Barrier() {
pthread_mutex_lock(&barrier);
numArrived++;
if (numArrived < numWorkers)
pthread_cond_wait(&go,
&barrier);
else {
numArrived =
0; /*last
worker awakens others */
pthread_cond_broadcast(&go);
}
pthread_mutex_unlock(&barrier);
}
void *Worker(void *);
int size,
stripSize;
/*size = stripSize*numWorkers
*/
int sums[MAXWORKERS];
/*sums computed by each worker
*/
int matrix[MAXSIZE][MAXSIZE];
/*read
command line, intitialize, and create threads
*/
int main(int argc, char *argv[]) {
int i, j;
pthread_attr_t
attr;
pthread_t workerid[MAXWORKERS];
/* set
global thread attributes */
pthread_attr_init(&attr);
pthread_attr_setscope(&attr,
PTHREAD_SCOPE_SYSTEM);
/*
initialize mutex and conditioon variable */
pthread_mutex_init(&barrier,
NULL);
pthread_cond_init(&go,
NULL);
/*read
command line */
size = atoi(argv[1]);
numWorkers =
atoi(argv[2]);
stripSize =
size/numWorkers;
/*initialize
the matrix */
for (i = 0;
i < size; i++)
for (j = 0; j < size; j++)
matrix[i][j] = 1;
/* create
the workers, then exit main thread */
for (i=0; i<numWorkers;
i++)
pthread_create(&worker[i], &attr, Worker, (void*) i);
pthread_exit(NULL);
}
/* each
worker sums the values in one strip
After a barrier, worker(0)
prints the total */
void *Worker(void *arg)
{
int myid = (int) arg;
int total, i, j, first,
last;
/*determine first and last
rows of my strip */
first = myid*stripSize;
last = first + stripSize
-1;
/*sum values in my strip
*/
total = 0;
for (i = first; i <=
last; i++)
for(j = 0; j
, size; j++)
total += matrix[i][j];
sumx[myid] = total;
Barrier();
if (myid = 0) { /*worker
0 computes the total */
total = 0;
for (i = 0;
i< numWorkers; i++)
total += sums[i];
printf("the
total is %d\n", total);
}
}