Pthreads in C: Difference between revisions

From RoggeWiki
Jump to navigation Jump to search
(Created page with 'One very popular API for threading an application is pthreads, also known as POSIX threads, P1003.1c, or ISO/IEC 9945-1:1990c. ====Data race==== THREAD 1 THREAD …')
 
No edit summary
Line 25: Line 25:


Pthreads use a data type called a mutex to achieve this.
Pthreads use a data type called a mutex to achieve this.
====Creating a POSIX thread====
Pthreads are created using pthread_create().
#include <pthread.h>
int
pthread_create (pthread_t *thread_id, const pthread_attr_t *attributes,
                void *(*thread_function)(void *), void *arguments);
This function creates a new thread. pthread_t is an opaque type which acts as a handle for the new thread. attributes is another opaque data type  which allows you to fine tune various parameters, to use the defaults pass NULL. thread_function is the function the new thread is executing, the thread will terminate when this function terminates, or it is explicitly killed. arguments is a void * pointer which is passed as the only argument to the thread_function.
Pthreads terminate when the function returns, or the thread can call pthread_exit() which terminates the calling thread explicitly.
int
pthread_exit (void *status);
status is the return value of the thread. (note a thread_function returns a void *, so calling return(void *) is the equivalent of this function.
One Thread can wait on the termination of another by using pthread_join()
int
pthread_join (pthread_t thread, void **status_ptr);
The exit status is returned in status_ptr.
A thread can get its own thread id, by calling pthread_self()
pthread_t
pthread_self ();
Two thread id's can be compared using pthread_equal()
int
pthread (pthread_t t1, pthread_t t2);
Returns zero if the threads are different threads, non-zero otherwise.

Revision as of 18:36, 17 December 2011

One very popular API for threading an application is pthreads, also known as POSIX threads, P1003.1c, or ISO/IEC 9945-1:1990c.

Data race

THREAD 1                THREAD 2
a = data;               b = data;
a++;                    b--;
data = a;               data = b;

Now if this code is executed serially (THREAD 1, the THREAD 2) there isn't a problem. However threads execute in an arbitrary order, so consider this:

THREAD 1                THREAD 2
a = data;
                       b = data;
a++;
                       b--;
data = a;
                       data = b;
[data = data - 1!!!!!!!]

So data could end up +1, 0, -1, and there is NO way to know which as it is completely non-deterministic!

In fact the problem is worse, because on many machines a = data is non-atomic. This means that when data is loaded into a, you could end up with the low order bits of the old data, and the high order bits of the new data. CHAOS.

The solution to this is to provide functions that will block a thread if another thread is accessing data that it is using.

Pthreads use a data type called a mutex to achieve this.

Creating a POSIX thread

Pthreads are created using pthread_create().

#include <pthread.h>
int 
pthread_create (pthread_t *thread_id, const pthread_attr_t *attributes,
                void *(*thread_function)(void *), void *arguments);

This function creates a new thread. pthread_t is an opaque type which acts as a handle for the new thread. attributes is another opaque data type which allows you to fine tune various parameters, to use the defaults pass NULL. thread_function is the function the new thread is executing, the thread will terminate when this function terminates, or it is explicitly killed. arguments is a void * pointer which is passed as the only argument to the thread_function.

Pthreads terminate when the function returns, or the thread can call pthread_exit() which terminates the calling thread explicitly.

int
pthread_exit (void *status);

status is the return value of the thread. (note a thread_function returns a void *, so calling return(void *) is the equivalent of this function.

One Thread can wait on the termination of another by using pthread_join()

int
pthread_join (pthread_t thread, void **status_ptr);

The exit status is returned in status_ptr.

A thread can get its own thread id, by calling pthread_self()

pthread_t
pthread_self ();

Two thread id's can be compared using pthread_equal()

int
pthread (pthread_t t1, pthread_t t2);

Returns zero if the threads are different threads, non-zero otherwise.