歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux文化 >> 原子操作的問題

原子操作的問題

日期:2017/2/27 12:09:34   编辑:Linux文化

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


Copyright © Linux教程網 All Rights Reserved