The simple programs threaddemo.cc (C++)
and threaddemo.c (C) illustrate
the creation and termination of threads. Also, it demonstrates the use
of barrier to synchronize the threads. This program creates some threads,
which run a function. Each thread computes some entries in a column of
a matrix using the previous column. Since the entries in a column depend
on the previous column, a thread must wait until all threads finish the
current column before it can start the next column. Barrier is used to
synchronize the threads. We provide the implementations of barrier in
barrier.hh (C++)
and barrier.cc (C++)
and barrier.h (C)
and and barrier.c (C).
Read them carefully and make sure
you understand them.
To compile the C++ program
% g++ -o ThreadDemo ThreadDemo.cc barrier.cc -lthread
To compile the C program
% g77 -o ThreadDemo ThreadDemo.c barrier.c -lthread
For information about thread, mutex, condition variable, see man page,
e.g., thr_create, mutex_init, cond_init
For information about thread programming and synchronization, see for
example
Multithreaded Programming Guide