>>> 此貼的回復 >> 會。 線程之間可以用 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等都不是特權指令