CS 326 Operating Systems

pthread.c

DownloadView Raw

/**
 * pthread.c
 *
 * Illustrates basic use of pthreads.
 *
 * Compile:  gcc -pthread -g -Wall -o pthread pthread.c
 */

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

/* Global variable: accessible to all threads */
int num;

void *hello(void * arg);

int main(int argc, char* argv[]) {

    num = 6;

    pthread_t thread;
    pthread_create(&thread, NULL, hello, (void *) 0);

    printf("Hello from the main thread\n");

    pthread_join(thread, NULL);

    printf("Bye! (from the main thread)\n");
    return 0;
}

void *hello(void *arg) {
    printf("Hello from child thread. Num is %d.\n", num);
    return NULL;
}