From ImageWiki

Revision as of 09:11, 25 September 2006 by Hauberg (Talk | contribs)
Jump to: navigation, search

The following simple program illustrates the basics of the POSIX Threads library (pthreads). The program doesn't really do anything except starting a thread that runs forever.

#include <pthread.h>

pthread_mutex_t my_mutex = PTHREAD_MUTEX_INITIALIZER;

void* some_thread(void *arg)
    while (true) {
        // Do something

int main(int argc, char *argv[]) 
    pthread_t thread;
    pthread_create(&thread, NULL, some_thread, NULL);

    // Do something (possibly join the thread)

    return 0;

When you need to link to PThreads on GNU/Linux, simply add -lpthread to your link falgs.

A simple tutorial on PThreads can be found at

Note: At some point, we need a better tutorial on this wiki.

Personal tools