Can a C/C++ compiler legally cache a variable in a register across a pthread library call?
问题 Suppose that we have the following bit of code: #include <pthread.h> #include <stdio.h> #include <stdlib.h> void guarantee(bool cond, const char *msg) { if (!cond) { fprintf(stderr, "%s", msg); exit(1); } } bool do_shutdown = false; // Not volatile! pthread_cond_t shutdown_cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t shutdown_cond_mutex = PTHREAD_MUTEX_INITIALIZER; /* Called in Thread 1. Intended behavior is to block until trigger_shutdown() is called. */ void wait_for_shutdown_signal() {