/* File: t6.c Facilitate threads alternating. (Would use pthread_yield, but not univerally supported.) */ #include // pthread_t, pthread_create #include // printf #include // sleep #define MAX 20 int x = 0; void* someFunc(void* unused) { fprintf(stderr, "Hi from someFunc\n"); for (int i = 0; i < MAX; ++i) { x++; fprintf(stderr, "."); sleep(1); } fprintf(stderr, "Bye from someFunc\n"); return NULL; } void* anotherFunc(void* unused) { fprintf(stderr, "Hi from anotherFunc\n"); for (int i = 0; i < MAX; ++i) { x--; fprintf(stderr, "o"); sleep(1); } fprintf(stderr, "Bye from anotherFunc\n"); return NULL; } int main() { pthread_t tid1, tid2; // thread id printf("About to create threads\n"); pthread_create(&tid1, NULL, someFunc, NULL); pthread_create(&tid2, NULL, anotherFunc, NULL); printf("Back from creating threads\n"); pthread_join(tid1, NULL); pthread_join(tid2, NULL); printf("x = %d\n", x); printf("main out\n"); }