Linux --- 原子操作

原子鎖是linux內核同步的一種機制,下面將其應用到線程同步中來。

#include <alsa/iatomic.h>
#include <pthread.h>
#include <stdio.h>

// 定義一個原子變量
static atomic_t g_atomic = ATOMIC_INIT(1);
// 定義共享資源
static volatile int g_i = 0;

/* 定義線程chǔ理函數 */
//#define atomic_dec_and_test(g_atomic) 1 
void *thr1_handle(void *arg)
{
    while (1) {
        if (atomic_dec_and_test(&g_atomic)) {
            printf("in thread %lu g_i = %d\n", pthread_self(), ++g_i);
        }
        atomic_inc(&g_atomic);
        sleep(1);
    }

    return NULL;   
}

void *thr2_handle(void *arg)
{
    while (1) {
        if (atomic_dec_and_test(&g_atomic)) {
            printf("in thread %lu g_i = %d\n", pthread_self(), --g_i);
        }
        atomic_inc(&g_atomic);
        sleep(1);
    }
    return NULL;   
}


/* 主函數 */
int main()
{
    pthread_t tid1, tid2;
    if (pthread_create(&tid1, NULL, thr1_handle, NULL) != 0) {
        fprintf(stderr, "create thread1 failed!\n");
        return 1;
    }
    if (pthread_create(&tid2, NULL, thr2_handle, NULL) != 0) {
        fprintf(stderr, "create thread2 failed!\n");
        return 2;
    }

    pthread_join(tid1, NULL);
    pthread_join(tid2, NULL);

    return 0;
}

不錯,這裏用 atomic 做了同步,程序如預期輸出: 
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
in thread 3067792192 g_i = 0
in thread 3076184896 g_i = 1
沒有任何問題。 
但是這裏最應該記住的是,如果不同不產生的後果。 
如果將上面的
//#define atomic_dec_and_test(g_atomic) 1
註釋打開,即不讓 atomic 來提供同步工作,那麼程序的結果是混luàn的。 如下: 
in thread 3075713856 g_i = 1           
in thread 3067321152 g_i = 0           
in thread 3067321152 g_i = -1           
in thread 3075713856 g_i = 1           
in thread 3067321152 g_i = 0           
in thread 3075713856 g_i = 1           
in thread 3075713856 g_i = 2      
in thread 3067321152 g_i = 0           
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章