__sync_add_and_get() 1亿次;
pthread_mutex_lock
add;
pthread_mutex_unlock 一亿次
8 inline void AddBySync()
9 {
10 int iTep;
11 iTep = __sync_add_and_fetch( &iAtomicVariable, 1 );
12
13 }
24 inline void addByMutex()
25 {
26 pthread_mutex_lock( &mutexLock );
27 iAtomicVariable++;
28 pthread_mutex_unlock( &mutexLock );
29
30 }
使用时间
inline AddBySync using time 1373850 us
inline addByMutex sing time using time 3390161 us;
结果分析发现
mutex一次操作位 33ns,sync_fetch 为13 ns
(sync using time 1403162 us, mutex using time 4269945 us )