pthread cond wait 用法分析

2022-05-30 01:51:14 字數 1263 閱讀 4306

很久沒看apue,今天一位朋友問道關於乙個mutex的問題,又翻到了以前討論過的東西,為了不讓自己忘記,把曾經的東西總結一下。 先大體看下網上很多地方都有的關於pthread_cond_wait()的說明:

現在來看一段典型的應用:看注釋即可。

[cpp]view plain

copy

#include 

#include 

static

pthread_mutex_t mtx = pthread_mutex_initializer;  

static

pthread_cond_t cond = pthread_cond_initializer;  

struct

node  *head = null;  

/*[thread_func]*/

static

void

cleanup_handler(

void

*arg)  

static

void

*thread_func(

void

*arg)  

p = head;  

head = head->n_next;  

printf("got %d from front of queue/n"

, p->n_number);  

free(p);  

pthread_mutex_unlock(&mtx);             //臨界區資料操作完畢,釋放互斥鎖

}  pthread_cleanup_pop(0);  

return

0;  

}  int

main(

void

)    

printf("thread 1 wanna end the line.so cancel thread 2./n"

);  

pthread_cancel(tid);             //關於pthread_cancel,有一點額外的說明,它是從外部終止子執行緒,子執行緒會在最近的取消點,退出執行緒,而在我們的**裡,最近的取消點肯定就是pthread_cond_wait()了。關於取消點的資訊,有興趣可以google,這裡不多說了

pthread_join(tid, null);  

printf("all done -- exiting/n"

);  

return

0;  

}  

pthread cond wait 用法分析

很久沒看apue,今天一位朋友問道關於乙個mutex的問題,又翻到了以前討論過的東西,為了不讓自己忘記,把曾經的東西總結一下。先大體看下網上很多地方都有的關於pthread cond wait 的說明 現在來看一段典型的應用 看注釋即可。include include static pthread ...

pthread cond wait 用法分析

現在來看一段典型的應用 看注釋即可。include include static pthread mutex t mtx pthread mutex initializer static pthread cond t cond pthread cond initializer struct node...

pthread cond wait 用法分析

很久沒看apue,今天一位朋友問道關於乙個mutex的問題,又翻到了以前討論過的東西,為了不讓自己忘記,把曾經的東西總結一下。先大體看下網上很多地方都有的關於pthread cond wait 的說明 現在來看一段典型的應用 看注釋即可。cpp view plain copy print inclu...