#include #include #include #include #include #include static MODIFIER int whose_turn_is_it = 0; _Thread_local int threadid; static void enter() { int me = threadid; while (whose_turn_is_it != me) continue; } static void leave() { int me = threadid; int other = 1 - me; whose_turn_is_it = other; } static long count; static void * thread_function (void *_id) { threadid = (uintptr_t) _id; for (int i = 0; i < 100000; i++) { enter(); count++; leave(); } } int main() { pthread_t t[2]; for (uintptr_t i = 0; i < 2; i++) pthread_create(&t[i], NULL, thread_function, (void *) i); for (int i = 0; i < 2; i++) { pthread_join(t[i], NULL); } printf("count %ld\n", count); return 0; }