Linux線程同步

來源:互聯網
上載者:User

標籤:線程

線程同步-互斥鎖  

1.初始化互斥鎖pthread_mutex_init() 

int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr); 

 

例: 

pthread_mutex_t mutex; 

pthread_mutex_init(&mutex, NULL); 

 

2.鎖住互斥鎖pthread_mutex_lock() 

int pthread_mutex_lock(pthread_mutex_t *mutex); 

當一個線程執行到此函數時,如果此鎖此時被另一個線程使用,那麼此線程被阻塞,知道另一個線程釋放此互斥鎖(第一次執行的線程可以運行,不會阻塞,後來的執行到此函數阻塞) 

成功返回0,否則返回錯誤號碼 

 

例:pthread_mutex_lock(&mutex); 

 

3.測試互斥鎖pthread_mutex_trylock() 

int pthread_mutex_trylock(pthread_mutex_t *mutex); 

測試完後加鎖,trylock後會鎖住 

錯誤返回-1 

 

4.解鎖 

pthread_mutex_unlock(&mutex); 

成功返回0,否則返回錯誤號碼 

 

5.銷毀互斥鎖 

pthread_mutex_destroy(&mutex); 

 

線程同步-條件變數  

自訂條件來阻塞進程 

1.初始化條件變數pthread_cond_init() 

int pthread_cond_int(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr); 

第一個參數是指向結構pthread_cond_t的指標,第二個參數cond_attr是一個指向結構pthread_condattr_t的指標。結構pthread_condattr_t是條件變數的屬性結構,和互斥鎖一樣我們可以用它來設定條件變數是進程內可用還是進程間可用,預設值是PTHREAD_PROCESS_PRIVATE,即此條件變數被同一進程內的各個線程使用 

例: 

pthread_cond_t cond; 

pthread_cond_init(&cond, NULL); 

 

2.阻塞線程pthread_cond_wait() 

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);(相當於在wait函數此處加一把鎖阻塞本線程,並釋放另一線程的互斥鎖,直到另一線程signal喚醒並unlock解鎖後才繼續運行,自動解鎖,等待完加鎖,執行到函數時只起阻塞作用,並解除鎖定) 

第一個參數是條件變數,第二個參數是線程阻塞的時候要解開的互斥鎖 

例:pthread_cond_wait(&cond, &mutex); 

 

3.喚醒線程pthread_cond_signal() 

int pthread_cond_signal(pthread_cond_t *cond); //喚醒處於wait狀態的線程,被喚醒的線程此時處於阻塞狀態,直到signal函數所線上程解除互斥鎖,wait函數所線上程才會繼續運行

int pthread_cond_broadcast(pthread_cond_t *cond); 

用來釋放被阻塞在條件變數cond上的線程,這兩個函數的區別在於當有多個線程都被同一個條件變數所阻塞時: 

用pthread_cond_broadcast()函數可以使所有線程都被喚醒 

用pthread_cond_signal()時哪一個線程被喚醒是由線程的調度策略所決定的 

例:pthread_cond_singal(&cond); 

 

4.銷毀條件變數 

例:pthread_cond_destroy(&cond); 

 

線程同步-訊號量  

1.初始化訊號量sem_init() 

int sem_init(sem_t *sem, int pshared, unsigned value); 

第一個參數為指向訊號量結構的指標 

第二個參數不為0時此訊號量在進程間共用,否則只能為當前進程的所有線程共用 

第三個參數給出訊號量的初值 

 

例: 

sem_t sem; 

sem_init(&sem, 0, 1); 

 

2.PV操作 

V操作 

int sem_post(sem_t *sem);  

參數是要增加的訊號量結構體的指標,當有線程阻塞在這個訊號量上時,調用sem_post()函數會使其中的一個線程解除阻塞,被解除阻塞的線程隨機播放機制同樣是由線程的調度策略決定的。當另外一個線程被解除後,訊號量又會被馬上減少到0 

 

P操作 

int sem_wait(sem_t *sem);  

int sem_trywait(sem_t *sem); 

函數sem_wait()被用來阻塞當前線程知道訊號量sem的值大於0,解除阻塞後將sem的值減1,表明公用資源經使用後減少 

函數sem_trywait()的區別是當訊號量等於0時,不會阻塞當前線程 

 

3.銷毀 

int sem_destroy(sem_t *sem); 

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Linux線程同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.