1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <pthread.h>
#include <stdio.h>

void* worker(void* arg) {
    int *p_x = (int*)arg;
    while(*p_x < 100) {
        (*p_x)++;
    }
    printf("increment finished\n");
    return NULL;
}
int main() {
    int x = 0, y = 0;
    printf("x: %d, y: %d\n", x, y);

    pthread_t t1, t2;
    pthread_create(&t1, NULL, worker, &x); // TODO: check if returns 0
    // like calling worker(&x)

    pthread_create(&t2, NULL, worker, &y); // TODO: check if returns 0
    // like calling worker(&y)

    // Three threads are active now.

    // Wait for them to finish.
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    printf("x: %d, y: %d\n", x, y);
    return 0;
}

This content is protected and may not be shared, uploaded, or distributed.    © 2019 Alexander J. Quinn.