previous | start | next

Creating Posix Threads

The system function to create a POSIX thread is:

 int pthread_create(pthread_t *restrict thread,
                    const pthread_attr_t *restrict attr,
                    void *(*start_routine)(void*), 
                    void *restrict arg);
   

Example:

#include <pthread.h>

void start() {
  pthread_t thread;
  int i;

  for(i = 0; i < n; i++) {
    pthread_create(&thread,   // thread ID
                   0,         // pointer to thread attribute struct
                   server,    // the thread's initial function
                   arg);      // the argument to be passed to the thread's fnc
  }
}

void * server(void *arg)
{
  // thread body
  return 0; // have to return a pointer
}


previous | start | next