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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int* a_n;
char marker;
} WorkerArgs;
void* worker(void* arg) {
WorkerArgs* a_args = arg;
int *a_x = a_args -> a_n;
while(*a_x < 100) {
(*a_x)++;
fputc(a_args -> marker, stdout); // print marker for this thread
fflush(stdout); // force it to print each character one at a time
}
printf("increment finished\n");
return NULL;
}
// GOAL: Increment x and y to 100, using only ++. Yes, this is silly.
int main() {
int x = 0, y = 0;
printf("x: %d, y: %d\n", x, y);
pthread_t t1; // This is an "opaque" objects -- none of your biz
WorkerArgs worker_args = { .a_n = &x, .marker='*' };
int status = pthread_create(&t1, NULL, worker, &worker_args);
// like calling worker(&x)
if(status != 0) {
return EXIT_FAILURE;
}
pthread_t t2;
worker_args = (WorkerArgs) { .a_n = &y, .marker='&' }; // BUG!!!!
// They each got the address of the same object. Same spot in
// memory.
status = pthread_create(&t2, NULL, worker, &worker_args);
if(status != 0) {
return EXIT_FAILURE;
}
// Three threads are active now: main thread, t1, and t2
// Wait for t1 to finish, and then join (terminate the thread nicely)
pthread_join(t1, NULL);
/* // If t2 finished before t1 finished, then t2 is waiting now. */
/* pthread_join(t2, NULL); */
printf("x: %d, y: %d\n", x, y);
return 0;
}
/*
// temporary:
int num_threads = 2;
pthread_t* threads = malloc(sizeof(*threads) * num_threads);
pthread_create(&threads[0], NULL, worker, &y); // TODO: check if returns 0
*/
|
© Copyright 2019 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.