Multiple treads


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

#ifndef NULL
#define NULL 0
#endif

#define NUMBER_OF_THREADS 10

void *print_hello_world(void *tid)
{
/* This function prints the thread’s identifier and then exits. */
    printf("Hello World. Greetings from thread %d\n", (int)tid);
    pthread_exit(NULL);
}

int main()
{
/* The main program creates 10 threads and then exits. */
    pthread_t threads[NUMBER_OF_THREADS];
    int status, i;
    for(i=0; i < NUMBER_OF_THREADS; i++) {
        printf("Main here. Creating thread %d\n", i);
        status = pthread_create(&threads[i], NULL, print_hello_world, (void *)i);
        if (status != 0) {
            printf("Oops. pthread create returned error code %d\n", status);
            exit(-1);
        }
    }
    sleep(5);
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 7 =