POSIX: Difference between revisions
Jump to navigation
Jump to search
Line 16: | Line 16: | ||
* Using ''thread-specific data'' area foreseen by POSIX, which requires first creating a key that is later used to set or get thread-specific data attached to that key (see [http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf Advanced Linux Programming - Ch 4]) |
* Using ''thread-specific data'' area foreseen by POSIX, which requires first creating a key that is later used to set or get thread-specific data attached to that key (see [http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf Advanced Linux Programming - Ch 4]) |
||
* Using keyword '''__thread''', see [http://www.akkadia.org/drepper/tls.pdf http://www.akkadia.org/drepper/tls.pdf] (not available in all compiler, not standard C99 [http://stackoverflow.com/questions/6869391/using-thread-in-c99]). |
* Using keyword '''__thread''', see [http://www.akkadia.org/drepper/tls.pdf http://www.akkadia.org/drepper/tls.pdf] (not available in all compiler, not standard C99 [http://stackoverflow.com/questions/6869391/using-thread-in-c99]). |
||
<source lang=c> |
|||
// 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; |
|||
} |
|||
</source> |
|||
=== Debugging === |
=== Debugging === |
Revision as of 22:38, 3 November 2012
PThreads
General Information
Links:
- POSIX Threads Programming, excellent introduction to pthreads, by Blaise Barney, at Lawrence Livermore National Laboratory.
- Advanced Linux Programming Ch 4 — Threads
- POSIX thread (pthread) libraries
- POSIX Threads (wikipedia)
- Advices
- 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.
Thread-Local Data
- Using thread-specific data area foreseen by POSIX, which requires first creating a key that is later used to set or get thread-specific data attached to that key (see Advanced Linux Programming - Ch 4)
- Using keyword __thread, see http://www.akkadia.org/drepper/tls.pdf (not available in all compiler, not standard C99 [1]).
// 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.