2. Thread Scheduling

t0.c (updated)
     1	#include <stdio.h>
     2	#include <stdlib.h>
     3	#include <pthread.h>
     4
     5	#include "common.h"
     6	#include "common_threads.h"
     7
     8	void *mythread(void *arg) {
     9	    printf("%s\n", (char *) arg);
    10	    return NULL;
    11	}
    12
    13	int main(int argc, char *argv[]) {
    14	    if (argc != 1) {
    15		fprintf(stderr, "usage: main\n");
    16		exit(1);
    17	    }
    18
    19	    pthread_t p1, p2;
    20	    printf("main: begin\n");
    21	    printf( "main: creating T1\n");
    22	    Pthread_create(&p1, NULL, mythread, "A");
    23	    printf( "main: creating T2\n");
    24	    Pthread_create(&p2, NULL, mythread, "B");
    25	    // join waits for the threads to finish
    26	    printf( "main: waiting for T1\n");
    27	    Pthread_join(p1, NULL);
    28	    printf( "main: waiting for T2\n");
    29	    Pthread_join(p2, NULL);
    30	    printf("main: end\n");
    31	    return 0;
    32	}
Figure 26.2: Simple Thread Creation Code (t0.c) (updated)
     
$ ./t0
main: begin
main: creating T1
main: creating T2
main: waiting for T1
A
B
main: waiting for T2
main: end

$ ./t0
main: begin
main: creating T1
main: creating T2
A
main: waiting for T1
main: waiting for T2
B
main: end

$ ./t0
main: begin
main: creating T1
main: creating T2
A
main: waiting for T1
B
main: waiting for T2
main: end