Posix Threads
Introduction
Example
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
pthread_create(pthread_t* , const pthread_attr_t *, void* (*job)(void*), void* args);
pthread_exit(void*);
pthread_join(pthread_t thread, void ** thread_return);
pthread_t pthread_self(void);
int pthread_equal(pthread_t thread1, pthread_t thread2);
comparison of two threads
int pthread_attr_init(pthread_attr_t * attr);
int pthread_attr_ ...
Mutexe (Mutual Exclusion Device)
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr);
int pthread_mutex_lock(pthread_mutex_t *mutex);
returns if lock successful otherwise blocks
int pthread_mutex_trylock(pthread_mutex_t *mutex);
returns whether lock is successful or not.
int pthread_mutex_unlock(pthread_mutex_t *mutex);
int pthread_mutex_destroy(pthread_mutex_t *mutex);