1 #include <pthread.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 #define NUM_THREADS 5
7
8 void *PrintHello(void *threadid)
9 {
10 sleep(10);
11 long tid;
12 tid = (long)threadid;
13 printf("Hello World! It's me, thread #%ld!\n", tid);
14 pthread_exit(NULL);
15 }
16
17 int main(int argc, char *argv[])
18 {
19 pthread_t threads[NUM_THREADS];
20 int rc;
21 long t;
22 for(t=0;t<NUM_THREADS;t++){
23 printf("In main: creating thread %ld\n", t);
24 rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
25 if (rc){
26 printf("ERROR; return code from pthread_create() is %d\n", rc);
27 exit(-1);
28 }
29 }
30
31 /* Last thing that main() should do */
32 pthread_exit(NULL);
33 }
|