POSIX Threads

From Wikipedia, the free encyclopedia

Jump to: navigation, search

POSIX Threads, or Pthreads, is a POSIX standard for threads. The standard defines an API for creating and manipulating threads. Pthreads are most commonly used on Unix-like POSIX systems such as Linux, Mac OS X and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API for the Windows 32-bit platform.[1]

Contents

[edit] Contents

Pthreads defines a set of C programming language types, functions and constants. It is implemented with a pthread.h header and a thread library. Programmers can use Pthreads to create, manipulate and manage threads, as well as synchronize between threads using mutexes and signals.

[edit] Example

An example of using Pthreads in C:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
 
static void wait_thread(void)
{
    time_t start_time = time(NULL);
 
    while (time(NULL) == start_time)
    {
        // do nothing except chew CPU slices for up to one second.
    }
}
 
static void *thread_func(void *vptr_args)
{
    int i;
 
    for (i = 0; i < 20; i++)
    {
        fputs("  b\n", stderr);
        wait_thread();
    }
 
    return NULL;
}
 
int main(void)
{
    int i;
    pthread_t thread;
 
    if (pthread_create(&thread, NULL, thread_func, NULL) != 0)
    {
        return EXIT_FAILURE;
    }
 
    for (i = 0; i < 20; i++)
    {
        fputs("a\n", stdout);
        wait_thread();
    }
 
    if (pthread_join(thread, NULL) != 0)
    {
        return EXIT_FAILURE;
    }
 
    return EXIT_SUCCESS;
}

An example of using Pthreads in C++:

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
 
class Thread{
	private:
		pthread_t thread;
		static void *thread_func(void *d){((Thread *)d)->run();}
	public:
		Thread(){}
                virtual ~Thread(){}
 
		virtual void run(){}
		int start(){return pthread_create(&thread, NULL, Thread::thread_func, (void*)this);}
		int wait(){return pthread_join(thread, NULL);}
};
 
int main(void){
 
	class Thread_a:public Thread{
		public:
			void run(){for(int i=0;i<20;i++){sleep(1);printf("a  \n");}}
	};
 
	class Thread_b:public Thread{
		public:
			void run(){for(int i=0;i<20;i++){sleep(1);printf("  b\n");}}
	};
 
	Thread *a=new Thread_a();
	Thread *b=new Thread_b();
 
	if (a->start() != 0) {
		return EXIT_FAILURE;
	}
 
	if (b->start() != 0) {
		return EXIT_FAILURE;
	}
 
	if (a->wait() != 0) {
		return EXIT_FAILURE;
	}
 
	if (b->wait() != 0) {
		return EXIT_FAILURE;
	}
 
 
	delete a;
	delete b;
 
	return EXIT_SUCCESS;
}

This program creates a new thread that prints lines containing 'b', while the main thread prints lines containing 'a'. The output is interleaved between 'a' and 'b' as a result of execution switching between the two threads, or simultaneous execution on a multicore system. In any case, the pattern of 'a's and 'b's does not strictly alternate between each letter, and can vary even between different runs on the same machine.

[edit] References

[edit] Further reading

[edit] See also

[edit] External links

Personal tools