>>> 此贴的回复 >> 会。 线程之间可以用 mutex 来互斥。 man pthread_mutex_init
CODE:[Copy to clipboard]EXAMPLE A shared global variable x can be protected by a mutex as follows:
int x; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
All accesses and modifications to x should be bracketed by calls to pthread_mutex_lock and pthread_mutex_unlock as follows:
pthread_mutex_lock(&mut); /* operate on x */ pthread_mutex_unlock(&mut);
>>> 此贴的回复 >> 可以自己实现atomic_t, 因为lock前缀和xchg、mfence等都不是特权指令