8.3. Pthread management

This section explains some of the basic Pthread management routines used for creating, joining, exiting, and detaching Pthreads.

8.3.1. Creating and terminating Pthreads

In order to create new Pthreads (besides the initial thread), the program must call the pthread_create() sub-routine. Example 8-5 is our first multi-threaded sample.

Example 8-5. create-5threads.c
						#include <pthread.h>                                                 /* #A */
#include <stdio.h>
#include <stdlib.h>
#define NUM_OF_THREADS      6
#define EXIT_CODE -1

void *thr_func(void *id)
{
    int j;

    for (j = 0; j < 500000; j++) {
        ;                                                            /* #B */
    }
    printf("Hello world from Pthread %d!\n", (int *)id);
    pthread_exit(NULL);                                              /* #C */ } int main (int argc, char *argv[]) { int rc, i; pthread_t tid[NUM_OF_THREADS]; for ...

Get Developing and Porting C and C++ Applications on AIX now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.