日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

linux线程操作

發(fā)布時(shí)間:2023/11/30 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux线程操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

初始化條件變量

int pthread_cond_init(pthread_cond_t *cv,pthread_cond_attr *cattr); 函數(shù)返回值:返回0表示成功,返回其他表示失敗。 參數(shù): pthread_cond_attr是用來設(shè)置pthread_cond_t的屬性,當(dāng)傳入的值是NULL的時(shí)候表示使用默認(rèn)的屬性。

函數(shù)返回時(shí),創(chuàng)建的條件變量保存在cv所指向的內(nèi)存中,可以用宏P(guān)THREAD_COND_INITIALIZER來初始化條件變量。值得注意的是不能使用多個(gè)線程初始化同一個(gè)條件變量,當(dāng)一個(gè)線程要使用條件變量的時(shí)候確保它是未被使用的。

?

條件變量的銷毀

int pthread_cond_destroy(pthread_cond_t *cv); 返回值:返回0表示成功,返回其他值表示失敗。

?

條件變量的使用:

int pthread_cond_wait(pthread_cond_t *cv,pthread_mutex_t *mutex) int pthread_cond_signal(pthread_cond_t *cv);

?

使用方式如下:

pthread_mutex_lock(&mutex) while or if(線程執(zhí)行的條件是否成立)pthread_cond_wait(&cond,&mutex); 線程執(zhí)行 pthread_mutex_unlock(&mutex);

?

?

為什么要加鎖

  • 線程在執(zhí)行的部分訪問的是進(jìn)程的資源,有可能多個(gè)線程需要訪問它,為了避免由于線程并發(fā)執(zhí)行所引起的資源競爭,所以要讓每個(gè)線程互斥的訪問公共資源。
  • 如果while或if判斷不滿足線程的執(zhí)行條件時(shí),線程回調(diào)用pthread_cond_wait阻塞自己。pthread_cond_wait被調(diào)用線程阻塞的時(shí)候,pthread_cond_wait會(huì)自動(dòng)釋放互斥鎖。線程從調(diào)用pthread_cond_wait到操作系統(tǒng)把他放在線程等待隊(duì)列之后的時(shí)候釋放互斥鎖。
  • ?

    使用while和if判斷線程執(zhí)行條件釋放成立的區(qū)別。

    在多線程資源競爭的時(shí)候,在一個(gè)使用資源的線程里面(消費(fèi)者)判斷資源是否可用,不可用便調(diào)用pthread_cond_wait,在另一個(gè)線程里面(生產(chǎn)者)如果判斷資源可用的話,則會(huì)調(diào)用pthead_cond_signal發(fā)送一個(gè)資源可用的信號。

    但是在wait成功之后,資源就不一定可以被使用,因?yàn)橥瑫r(shí)有兩個(gè)或兩個(gè)以上的線程正在等待次資源,wait返回后,資源可能已經(jīng)被使用了,在這種情況下

    while(resource == FALSE)pthread_cond_wait(&cond,&mutex);

    如果之后只有一個(gè)消費(fèi)者,就可使用if。

    分解pthread_cond_wait動(dòng)作為以下步驟:

  • 線程放在等待隊(duì)列上,解鎖
  • 等待pthread_cond_signal或者pthread_cond_broadcast信號之后去競爭鎖
  • 若競爭到互斥鎖則加鎖
  • ?

    有可能多個(gè)線程在等待這個(gè)資源可用的信號,信號發(fā)出去之后只有一個(gè)資源可用,但是有A,B兩個(gè)線程在等待,B速度比較快,獲得互斥鎖,然后加鎖,消耗資源,然后解鎖,之后A獲得互斥鎖,但它回去發(fā)現(xiàn)資源已經(jīng)被使用了,它便有兩個(gè)選擇,一個(gè)失去訪問不存在的資源,另一個(gè)就是繼續(xù)等待,那么等待下去的條件就是使用while,要不然使用if的話pthread_cond_wait返回后,就會(huì)順序執(zhí)行下去。

    ?

    等待線程:

    pthread_cond_wait????? 前要加鎖

    pthread_cond_wait????? 內(nèi)部會(huì)解鎖,然后等待條件變量被其他線程激活

    pthread_cond_wait????? 被激活后會(huì)再自動(dòng)加鎖

    ?

    激活線程

    加鎖(和等待線程用同一個(gè)鎖)

    pthread_cond_signal?? 發(fā)送信號(階躍信號前最后判斷有無等待線程)

    解鎖

    激活線程的上面三個(gè)操作再運(yùn)行時(shí)間上都是再等待線程的pthread_cond_wait函數(shù)內(nèi)部。

    /*** pthread_if.c ***/ #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;int count = 0;void *decrement(void *arg) {printf("in derment\n");pthread_mutex_lock(&mutex);if(count == 0)pthread_cond_wait(&cond,&mutex);count--;printf("----decrement:%d\n",count);printf("out decrement\n");pthread_mutex_unlock(&mutex);return NULL; }void *increment(void *arg) {printf("in increment\n");pthread_mutex_lock(&mutex);count++;printf("-----increment:%d\n",count);if(count != 0){pthread_cond_signal(&cond);}printf("out increment\n");pthread_mutex_unlock(&mutex);return NULL; }int main() {pthread_t tid_in,tid_de;pthread_create(&tid_de,NULL,(void*)decrement,NULL);sleep(1);pthread_create(&tid_in,NULL,(void*)increment,NULL);sleep(1);pthread_join(tid_de,NULL);pthread_join(tid_in,NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0; }

    ?

    ?

    /*** pthread_while.c ***/ #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<unistd.h>typedef struct node_s {int data;struct node_s *next; }node_t;node_t *head = NULL;pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;void cleanup_handler(void *arg) {printf("cleanup_handler is running.\n");free(arg);pthread_mutex_unlock(&mutex); }void *thread_func(void *arg) {node_t *p = NULL;pthread_cleanup_push(cleanup_handler,p);while(1){pthread_mutex_lock(&mutex);while(NULL == head)pthread_cond_wait(&cond,&mutex);p = head;head = head->next;printf("process %d node\n",p->data);free(p);pthread_mutex_unlock(&mutex);}pthread_cleanup_pop(0);return NULL; }int main() {pthread_t tid;node_t *temp = NULL;int i;pthread_create(&tid,NULL,(void*)thread_func,NULL);for(i = 0; i < 10; i++){temp = (node_t*)malloc(sizeof(node_t));temp->data = i;pthread_mutex_lock(&mutex);temp->next = head;head = temp;pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);sleep(1);}pthread_cancel(tid);pthread_join(tid,NULL);return 0;}

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/wanghao-boke/p/11613064.html

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的linux线程操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。