亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語(yǔ)言細(xì)致講解線程同步的集中方式

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

互斥鎖

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

例向屏幕上一次打印abcd四個(gè)字母

可以使用的是一個(gè)類(lèi)似鎖連的思想 a 加完解開(kāi)后拿b鎖依次類(lèi)推

#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);
    }
}

條件變量

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

#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號(hào)線程生產(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號(hào)線程消費(fèi)%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);
}

信號(hào)量

介紹以下信號(hào)量是進(jìn)化版的互斥量,允許多個(gè)線程訪問(wèn)共享資源與條件變量和互斥量類(lèi)此的操作,在進(jìn)程和線程中均可以使用

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

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

pshared

  • 0 代表線程信號(hào)量
  • 1 代表進(jìn)程信號(hào)量

alue 為定義的信號(hào)量個(gè)數(shù)

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

申請(qǐng)信號(hào)量,申請(qǐng)成功value–,當(dāng)value為0 則阻塞

 int sem_post(sem_t *sem);

釋放信號(hào)量value++

例 信號(hào)量實(shí)現(xiàn)生產(chǎn)者消費(fèi)者模型

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)者申請(qǐng)資源 pro_sem每被占用一次--一次 當(dāng)為0時(shí)則阻塞
        printf("%ld 線程生產(chǎn)了 %d\n",pthread_self(),beginnum);
        queue[(i++)%semcnt]= beginnum++;
        sem_post(&con_sem);//為消費(fèi)者的信號(hào)量釋放資源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 線程消費(fèi)了 %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); //消費(fèi)者初始默認(rèn)沒(méi)有產(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);
}

讀寫(xiě)鎖

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

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

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

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

讀寫(xiě)鎖的特性: 讀鎖可以共享讀的狀態(tài),當(dāng)讀鎖加上時(shí),阻塞寫(xiě)鎖的加鎖

即使讀鎖加上時(shí) 后面的 寫(xiě)鎖依然會(huì)被阻塞,當(dāng)前面讀鎖釋放時(shí)才能加成功

pthread_rwlock_t rwlock =PTHREAD_RWLOCK_INITIALIZER;
int beginum=100;
void*thr_Wr(void*arg)
{
    while(1)
    {
        pthread_rwlock_wrlock(&rwlock);
        printf("-寫(xiě)線程--beginum = %d\n",beginum++);
        usleep(2000);//模擬占用時(shí)間
        pthread_rwlock_unlock(&rwlock);
        usleep(2000);//簡(jiǎn)單防止再搶鎖的方法但不建議使用
    }
    return NULL;
}
void*thr_ead(void*arg)
{
    while (1)
    {
        pthread_rwlock_rdlock(&rwlock);
      printf("-讀讀線程--beginum = %d\n",beginum);
        usleep(2000);//模擬占用時(shí)間
       pthread_rwlock_unlock(&rwlock);
        usleep(2000);//簡(jiǎn)單防止再搶鎖的方法但不建議使用
    }
    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語(yǔ)言細(xì)致講解線程同步的集中方式的文章就介紹到這了,更多相關(guān)C語(yǔ)言線程同步內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++實(shí)現(xiàn)高性能轉(zhuǎn)換大小寫(xiě)算法示例

    C++實(shí)現(xiàn)高性能轉(zhuǎn)換大小寫(xiě)算法示例

    大小寫(xiě)轉(zhuǎn)換是我們作為一名程序員經(jīng)常會(huì)遇到,也必須要會(huì)的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于C++實(shí)現(xiàn)高性能轉(zhuǎn)換大小寫(xiě)算法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2018-01-01
  • C++中的復(fù)制構(gòu)造函數(shù)詳解

    C++中的復(fù)制構(gòu)造函數(shù)詳解

    今天小編就為大家分享一篇關(guān)于關(guān)于C++復(fù)制構(gòu)造函數(shù)的實(shí)現(xiàn)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2021-09-09
  • C++隨機(jī)生成迷宮算法

    C++隨機(jī)生成迷宮算法

    這篇文章主要為大家詳細(xì)介紹了C++隨機(jī)生成迷宮算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C語(yǔ)言中十六進(jìn)制轉(zhuǎn)十進(jìn)制兩種實(shí)現(xiàn)方法

    C語(yǔ)言中十六進(jìn)制轉(zhuǎn)十進(jìn)制兩種實(shí)現(xiàn)方法

    這篇文章主要介紹了C語(yǔ)言中十六進(jìn)制轉(zhuǎn)十進(jìn)制兩種實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • C語(yǔ)言聯(lián)合體Union特點(diǎn)及運(yùn)用全面講解教程

    C語(yǔ)言聯(lián)合體Union特點(diǎn)及運(yùn)用全面講解教程

    這篇文章主要為大家介紹了C語(yǔ)言聯(lián)合體Union特點(diǎn)及運(yùn)用的全面講解教程有需要深度朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2021-10-10
  • C++實(shí)現(xiàn)拷貝構(gòu)造函數(shù)的方法詳解

    C++實(shí)現(xiàn)拷貝構(gòu)造函數(shù)的方法詳解

    拷貝構(gòu)造函數(shù)是構(gòu)造函數(shù)的一個(gè)重載,因此顯式的定義了拷貝構(gòu)造,那么編譯器也不再默認(rèn)生成構(gòu)造函數(shù)。本文主要介紹了C++實(shí)現(xiàn)拷貝構(gòu)造函數(shù)的方法,需要的可以參考一下
    2022-09-09
  • C語(yǔ)言中 printf 函數(shù)輸出格式

    C語(yǔ)言中 printf 函數(shù)輸出格式

    這篇文章主要介紹了C語(yǔ)言中 printf 函數(shù)簡(jiǎn)介,通過(guò)實(shí)例代碼給大家介紹Printf輸出格式的相關(guān)知識(shí),需要的朋友可以參考下
    2021-08-08
  • C++嵌套類(lèi)與局部類(lèi)詳細(xì)解析

    C++嵌套類(lèi)與局部類(lèi)詳細(xì)解析

    從作用域的角度看,嵌套類(lèi)被隱藏在外圍類(lèi)之中,該類(lèi)名只能在外圍類(lèi)中使用。如果在外圍類(lèi)之外的作用域使用該類(lèi)名時(shí),需要加名字限定
    2013-09-09
  • 深入uCOS中全局變量的使用詳解

    深入uCOS中全局變量的使用詳解

    本篇文章是對(duì)uCOS中全局變量的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)順序表的進(jìn)階講解

    C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)順序表的進(jìn)階講解

    程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示
    2022-04-04

最新評(píng)論