Programming POSIX threads on Linux
Part I: Thread basics
On this page, I want to give a very short overview over the programming of POSIX threads on a
Linux box. At the moment, my perspective lays on a working example, not a complete tutorial
(for such, please have a look at the references), and the prerequisites necessary
for compiling and linking the example on Linux. So if you know a bit what's going on with the
pthread library and just look for the right compiler switch, or you have already programmed
threads, but forgotten which parameter is the thread attribute struct, your are right here :-)
The source code
Now first let's have a look to the source code of a real program, thread.c.
It does not do neither much nor exciting things,
just sleeps for a random time interval. The special is, that the sleeping is done in a separate
thread, and the number of threads can be controlled by the command line. The source code is written
in C and can be downloaded here.
Compiling and linking
For your commodity, you can compile and link the program with the following
Makefile, which is not
the smallest possible version (in fact, I cloned it from another project, feel free to
minimize it by yourself).
With this Makefile in your working directory, you can simply type make to create
or re-create the executable thread.
Usage
The program is started by a command line:
thread 5
This call lets the program create 5 threads which will wait a random time interval. The main
line of the program will wait for each thread, in the same order the threads are created.
This is necessary, because only after joining the thread to the main line via pthread_join,
you can catch the thread's return value.
Unfortunately, it is only possible to wait for termination of a
specified thread, not for one of a set of threads, so we will wait for the longest-running
thread to complete, even if all other are already finished. This may be a problem in
some applications, so please have a look at part II of this article,
where a condition variable is used to overcome this limitation of pthread_join.
Programmer's quick reference
Each program using POSIX threads has to include the following header file:
#include <pthread.h>
To succesfully compile and link the program, you have to speficy the following compiler
switches and libraries, which are included in the Makefile:
-D_REENTRANT % compiler switch
-lpthreads % linker lib
Here are the most important POSIX functions concerning the basics of thread programming.
For a quick reference, they are given as function prototypes. The usage is shown in the
example source code.
int pthread_create(pthread_t* tid, const pthread_attr_t* attr,
void* (*func)(void* arg), void* arg);
return value: 0 if OK, positive error code otherwise
int pthread_join(pthread_t tid, void** status);
return value: 0 if OK, positive error code otherwise
pthread_t pthread_self(void);
return value: thread ID of the calling thread
int pthread_detach(pthread_t tid);
return value: 0 if OK, positive error code otherwise
void pthread_exit(void* status);
no return to caller :-)
Literature
- R. W. Stevens, Unix Network Programming, vol. II: Interprocess Communications,
Prentice-Hall.
A very good book in two volumes which can be seen as classic text about every aspect of
Unix network programming, including sockets, threads, IPC and RPC.
Online ressourcen
There are quite a lot of good web sites offering detailed information about POSIX threads. A
very incomplete selection follows:
|