Introduce examples dir
[lcapit-junk-code.git] / examples / C / thread.c
blob40e091b63e8c196db5cbcae314c0389f69a3f531
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
5 void *thr_fn1(void *arg)
7 printf("thread 1 returning\n");
8 return((void *) 1);
11 void *thr_fn2(void *arg)
13 printf("thread 2 exiting\n");
14 pthread_exit((void *) 2);
17 int main(void)
19 int err;
20 pthread_t tid1, tid2;
21 void *tret;
23 err = pthread_create(&tid1, NULL, thr_fn1, NULL);
24 if (err != 0) {
25 fprintf(stderr, "can't create thread 1: %s\n", strerror(err));
26 exit(1);
29 err = pthread_create(&tid2, NULL, thr_fn2, NULL);
30 if (err != 0) {
31 fprintf(stderr, "can't create thread 2: %s\n", strerror(err));
32 exit(1);
35 err = pthread_join(tid1, &tret);
36 if (err != 0) {
37 fprintf(stderr, "can't join with thread 1: %s\n",
38 strerror(err));
39 exit(1);
41 printf("thread 1 exit code: %d\n", (int) tret);
43 err = pthread_join(tid2, &tret);
44 if (err != 0) {
45 fprintf(stderr, "can't join with thread 2: %s\n",
46 strerror(err));
47 exit(1);
49 printf("thread 2 exit code: %d\n", (int) tret);
51 return 0;