最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言細致講解線程同步的集中方式

 更新時間:2022年05月09日 10:50:50   作者:Gy648  
多線程中的線程同步可以使用,CreateThread,CreateMutex 互斥鎖實現(xiàn)線程同步,通過臨界區(qū)實現(xiàn)線程同步,Semaphore 基于信號實現(xiàn)線程同步,CreateEvent 事件對象的同步,以及線程函數(shù)傳遞單一參數(shù)與多個參數(shù)的實現(xiàn)方式

互斥鎖

使用互斥量完成對臨界區(qū)的資源的加鎖操作,使得同一時刻,對一個共享數(shù)據(jù)的使用只能又一個線程完成

例向屏幕上一次打印abcd四個字母

可以使用的是一個類似鎖連的思想 a 加完解開后拿b鎖依次類推

#define THRNUM 4
static pthread_mutex_t mut[4];
static int next(int n)
{
    if(n + 1 == THRNUM)
    return 0;
    return n+1;
}
static void* pthreadfunc(void* p)
{
    int n =(int)p;
    char c = 'a' + (int)p;
    while(1)
    {
    pthread_mutex_lock(mut + n);
    write(1,&c,1);
    pthread_mutex_unlock(mut + next(n));
    }
    pthread_exit(NULL);
}
int main()
{
    int i,err;
    pthread_t tid[THRNUM];
    //創(chuàng)建線程
    for(i = 0 ; i < THRNUM ;i++){
        //初始化鎖
        pthread_mutex_init(mut + i,NULL);
        //加鎖
        pthread_mutex_lock(mut+i );
    err = pthread_create(tid+i,NULL,pthreadfunc,(void*)i );
    if(err != 0)
    {
        fprintf(stderr,"create:%s\n",strerror(err));
        exit(1);
    }
    }
    //回收線程
    pthread_mutex_unlock(mut + 0);
    alarm(5);
    for(i = 0 ; i < THRNUM ;i++){
    pthread_join(tid+i,NULL);
    }
}

條件變量

條件變量并不是鎖而是一種阻塞機制,使得我們的程序在某些特定的條件,比如生產(chǎn)者生產(chǎn)達到上限未消費,此時使用條件變量(加上while對條件的判斷)來阻塞生產(chǎn),讓生產(chǎn)者消費

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<string.h>
int begnum=0;
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;
typedef struct _prodinfo
{
    int num;
    struct _prodinfo *next;
}prod;
struct _prodinfo* head=NULL;
/*  條件變量可以引起阻塞并非鎖
*/
void *thr_produce(void*arg)
{
    while(1)
    {
        prod* pd = malloc(sizeof(struct _prodinfo));
        pd->num=begnum++;
        pthread_mutex_lock(&mut);
        pd->next=head;
        head=pd;
        printf(" -%ld號線程生產(chǎn)%d產(chǎn)品\n",pthread_self(),pd->num);
        pthread_mutex_unlock(&mut);
        pthread_cond_signal(&cond);  
        sleep(rand()%4);
    }
}
void* thr_con(void* arg)
{
    prod* pro=NULL;
    while(1)
    {
        pthread_mutex_lock(&mut);
        while(head==NULL)
            pthread_cond_wait(&cond,&mut);
        pro = head;
        head=head->next;
        printf(" -%ld號線程消費%d產(chǎn)品\n",pthread_self(),pro->num);
        pthread_mutex_unlock(&mut);
        free(pro);
        sleep(rand()%4);
    }
}
int main()
{
    pthread_t cid,pid;
    int err1=pthread_create(&pid,NULL,thr_produce,NULL);
     if(err1)
        {
            fprintf(stderr,"pthread_creat():%s\n",strerror(err1));
            exit(1);
        }
    int err2=pthread_create(&cid,NULL,thr_con,NULL);
      if(err2)
        {
            fprintf(stderr,"pthread_creat():%s\n",strerror(err1));
            exit(1);
        }
    pthread_join(pid,NULL);
    pthread_join(cid,NULL);
}

信號量

介紹以下信號量是進化版的互斥量,允許多個線程訪問共享資源與條件變量和互斥量類此的操作,在進程和線程中均可以使用

int sem_init(sem_t *sem, int pshared, unsigned int value);
int sem_destroy(sem_t *sem);
Link with -pthread.

sem為定義的信號量,傳出型參數(shù)

pshared

  • 0 代表線程信號量
  • 1 代表進程信號量

alue 為定義的信號量個數(shù)

    int sem_wait(sem_t *sem);
    int sem_trywait(sem_t *sem);
    int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);

申請信號量,申請成功value–,當value為0 則阻塞

 int sem_post(sem_t *sem);

釋放信號量value++

例 信號量實現(xiàn)生產(chǎn)者消費者模型

sem_t pro_sem,con_sem;
#define semcnt 5
int i=0;
int queue[semcnt];
int beginnum = 100;
void *thr_produce(void*arg)
{
    while(1)
    {
        sem_wait(&pro_sem);//生產(chǎn)者申請資源 pro_sem每被占用一次--一次 當為0時則阻塞
        printf("%ld 線程生產(chǎn)了 %d\n",pthread_self(),beginnum);
        queue[(i++)%semcnt]= beginnum++;
        sem_post(&con_sem);//為消費者的信號量釋放資源pro_sem每被釋放一次++一次
        sleep(rand()%4);
    }
    return NULL;
}
void* thr_con(void* arg)
{   
    int i=0;
    int num=0;
    while(1)
    {
        sem_wait(&con_sem);
        num = queue[(i++)%semcnt];
        printf("%ld 線程消費了 %d\n",pthread_self(),num);
        sem_post(&pro_sem);
        sleep(rand()%3);
    }
    return NULL;
}
int main()
{
    sem_init(&pro_sem,0,semcnt);
    sem_init(&con_sem,0,0); //消費者初始默認沒有產(chǎn)品
    pthread_t tid[2];
     int err1=pthread_create(&tid[0],NULL,thr_produce,NULL);
     if(err1)
        {
            fprintf(stderr,"pthread_creat():%s\n",strerror(err1));
            exit(1);
        }
    int err2=pthread_create(&tid[1],NULL,thr_con,NULL);
      if(err2)
        {
            fprintf(stderr,"pthread_creat():%s\n",strerror(err1));
            exit(1);
        }
        pthread_join(tid[0],NULL);
        pthread_join(tid[1],NULL);
    sem_destroy(&pro_sem);
    sem_destroy(&con_sem);
}

讀寫鎖

讀寫鎖 與互斥量類似,但是讀寫鎖允許更高的并行性,其特性為:寫?yīng)氄?,讀共享

讀寫鎖實質(zhì)上是一把鎖,有不同的狀態(tài),寫鎖的優(yōu)先級高

讀寫鎖的三種狀態(tài)

  • 讀模式下加鎖(讀鎖)
  • 寫模式下加鎖(寫鎖)
  • 不加鎖狀態(tài)

讀寫鎖的特性: 讀鎖可以共享讀的狀態(tài),當讀鎖加上時,阻塞寫鎖的加鎖

即使讀鎖加上時 后面的 寫鎖依然會被阻塞,當前面讀鎖釋放時才能加成功

pthread_rwlock_t rwlock =PTHREAD_RWLOCK_INITIALIZER;
int beginum=100;
void*thr_Wr(void*arg)
{
    while(1)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("-寫線程--beginum = %d\n",beginum++);
        usleep(2000);//模擬占用時間
        pthread_rwlock_unlock(&rwlock);
        usleep(2000);//簡單防止再搶鎖的方法但不建議使用
    }
    return NULL;
}
void*thr_ead(void*arg)
{
    while (1)
    {
        pthread_rwlock_rdlock(&rwlock);
      printf("-讀讀線程--beginum = %d\n",beginum);
        usleep(2000);//模擬占用時間
       pthread_rwlock_unlock(&rwlock);
        usleep(2000);//簡單防止再搶鎖的方法但不建議使用
    }
    return NULL;
}
int main()
{
    int n=8,i=0;
    pthread_t tid[8];
    for(i = 0; i<5;i++)
    {
        pthread_create(&tid[i],NULL,thr_ead,NULL);
    }
     for(; i<8;i++)
    {
        pthread_create(&tid[i],NULL,thr_Wr,NULL);
    }
     for(i = 0; i<8;i++)
    {
        pthread_join(tid[i],NULL);
    }
   pthread_rwlock_destroy(&rwlock);
}

到此這篇關(guān)于C語言細致講解線程同步的集中方式的文章就介紹到這了,更多相關(guān)C語言線程同步內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++實現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項)

    C++實現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項)

    這篇文章主要介紹了C++實現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • C++中各種可調(diào)用對象深入講解

    C++中各種可調(diào)用對象深入講解

    這篇文章主要給大家介紹了關(guān)于C++中各種可調(diào)用對象的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • C++?Qt實現(xiàn)音視頻播放功能

    C++?Qt實現(xiàn)音視頻播放功能

    Qt版本?5.9?基于C++11?Qt核心組件與附加組件安裝時請打鉤?否則可能出現(xiàn)項目中缺少視頻播放模塊的問題,由于最近著手的Qt項目需要視頻播放自己做的時候踩很多坑避免以后踩坑,故在此記錄實現(xiàn)過程,感謝的朋友參考下吧
    2021-11-11
  • 用C語言判斷字符是否為空白字符或特殊字符的方法

    用C語言判斷字符是否為空白字符或特殊字符的方法

    這篇文章主要介紹了用C語言判斷字符是否為空白字符或特殊字符的方法,分別為isspace()函數(shù)的使用和ispunct()函數(shù)的使用,需要的朋友可以參考下
    2015-08-08
  • VC++中HTControl控件類之CHTRichEdit富文本編輯控件實例

    VC++中HTControl控件類之CHTRichEdit富文本編輯控件實例

    這篇文章主要介紹了VC++中HTControl控件類之CHTRichEdit富文本編輯控件,是一個比較實用的功能,需要的朋友可以參考下
    2014-08-08
  • C++浮點數(shù)類型詳情

    C++浮點數(shù)類型詳情

    這篇文章主要介紹了C++浮點數(shù)類型,浮點數(shù)是C++的第二組基本類型,它能夠表示帶小數(shù)部分的數(shù)字。不僅如此,浮點數(shù)的范圍也比int更大,可以表示更大范圍的數(shù)字。下面來我們大家一起來學(xué)習(xí)學(xué)習(xí)內(nèi)容
    2021-11-11
  • C++實現(xiàn)推箱子小游戲

    C++實現(xiàn)推箱子小游戲

    這篇文章主要為大家詳細介紹了C++實現(xiàn)推箱子小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-01-01
  • C++ 多重繼承和虛擬繼承對象模型、效率分析

    C++ 多重繼承和虛擬繼承對象模型、效率分析

    本文簡單介紹多態(tài)和多重繼承、虛擬繼承的基本概念。隨后重點分析了C++中對象模型之間的差異和運行效率
    2014-08-08
  • C語言學(xué)習(xí)之指針的使用詳解

    C語言學(xué)習(xí)之指針的使用詳解

    想突破C語言的學(xué)習(xí),對指針的掌握是非常重要的,本文為大家總結(jié)了C語言中指針的相關(guān)知識點,文中的示例代碼講解詳細,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-10-10
  • C++ Qt開發(fā)之使用QHostInfo查詢主機地址

    C++ Qt開發(fā)之使用QHostInfo查詢主機地址

    Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應(yīng)用程序,本文將重點介紹如何運用QHostInfo組件實現(xiàn)對主機地址查詢功能,希望對大家有所幫助
    2024-03-03

最新評論

东明县| 桑日县| 夏津县| 胶南市| 钟祥市| 福安市| 南溪县| 巨鹿县| 湖州市| 马尔康县| 新郑市| 新乡县| 远安县| 田林县| 甘泉县| 寻甸| 和政县| 安多县| 东丽区| 汽车| 云南省| 曲水县| 达孜县| 通州市| 临清市| 甘肃省| 萍乡市| 彭州市| 介休市| 长阳| 黄梅县| 长子县| 炉霍县| 土默特左旗| 库伦旗| 仙桃市| 镇江市| 根河市| 日照市| 嘉义县| 星座|