POSIX: Difference between revisions

From miki
Jump to navigation Jump to search
Line 16: Line 16:
; Joinable / detachable state
; Joinable / detachable state
: For portability, always create ''joinable'' or ''detachable'' threads by setting explicitly the thread attribute (using <code>pthread_attr_getdetachstate</code>). This provides portability as not all implementations may create threads as joinable by default.
: For portability, always create ''joinable'' or ''detachable'' threads by setting explicitly the thread attribute (using <code>pthread_attr_getdetachstate</code>). This provides portability as not all implementations may create threads as joinable by default.

=== Linux vs. POSIX manual pages ===
Most manual pages on pthreads are available as either standard linux manual pages or as extract from the POSIX programmer manual.

For instance:
* [http://localhost/cgi-bin/dwww?type=runman&location=pthread_cond_signal/3posix pthread_cond_signal(3posix)]
* [http://localhost/cgi-bin/dwww?type=runman&location=pthread%5fcond%5fsignal/3 pthread_cond_signal(3)]

Both versions are worth reading.

=== Create threads ===
See [http://localhost/cgi-bin/dwww?type=runman&location=pthread%5fcreate/3 pthread_create(3)].

=== Conditional variables ===
See
* pthread_cond_init
* pthread_cond_destroy
* pthread_cond_signal
* pthread_cond_broadcast
* pthread_cond_wait
* pthread_cond_timedwait

Note that these functions are '''NOT''' ''signal-async safe'' (see [http://localhost/cgi-bin/dwww?type=runman&location=signal/7], [http://localhost/cgi-bin/dwww?type=runman&location=pthread%5fcond%5fsignal/3]). They '''CANNOT''' be called safely from a async signal handler (like ''SIGIO'' signal handler).


=== Thread-Local Data ===
=== Thread-Local Data ===

Revision as of 23:20, 3 November 2012

PThreads

General Information

Links:

Manual page:

Portability

Joinable / detachable state
For portability, always create joinable or detachable threads by setting explicitly the thread attribute (using pthread_attr_getdetachstate). This provides portability as not all implementations may create threads as joinable by default.

Linux vs. POSIX manual pages

Most manual pages on pthreads are available as either standard linux manual pages or as extract from the POSIX programmer manual.

For instance:

Both versions are worth reading.

Create threads

See pthread_create(3).

Conditional variables

See

  • pthread_cond_init
  • pthread_cond_destroy
  • pthread_cond_signal
  • pthread_cond_broadcast
  • pthread_cond_wait
  • pthread_cond_timedwait

Note that these functions are NOT signal-async safe (see [1], [2]). They CANNOT be called safely from a async signal handler (like SIGIO signal handler).

Thread-Local Data

// Compile this code with : gcc local.c -o local -lpthread

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

static __thread int myint;
static int globalint = 0;

void * handler(void *arg)
{
    myint = ++globalint;                              // thread unsafe - should use a mutex
    while(1) {
        printf("myint is %d\n", myint);
        sleep(1);
    }
}

int main (int argc, char **argv)
{
    pthread_t p1,p2;

    pthread_create(&p1, NULL, handler, NULL);
    pthread_create(&p2, NULL, handler, NULL);

    pause();
    return 0;
}

Debugging

See Debugging page.