initial import
[staff/goc4/2018bti7061.git] / c-6-additional / examples / c-paralelism / thread0.c
1 #include <pthread.h>
2 #include <stdio.h>
3 /* Source
4 http://timmurphy.org/2010/05/04/pthreads-in-c-a-minimal-working-example/
5 */
6
7 void *inc_x(void *x_void_ptr){
8 int *x_ptr = (int *)x_void_ptr;
9 while(++(*x_ptr) < 100);
10 printf("x increment finished\n");
11 return NULL;
12 }
13 int main(){
14 int x = 0, y = 0;
15 printf("x: %d, y: %d\n", x, y);
16 pthread_t inc_x_thread;
17 if(pthread_create(&inc_x_thread, NULL, inc_x, &x)) {
18 fprintf(stderr, "Error creating thread\n");
19 return 1;
20 }
21 while(++y < 100);
22 printf("y increment finished\n");
23 if(pthread_join(inc_x_thread, NULL)) {
24 fprintf(stderr, "Error joining thread\n");
25 return 2;
26 }
27 printf("x: %d, y: %d\n", x, y);
28 return 0;
29 }