linux的C使用pthread_mutex互斥锁和条件变量

互斥概念

所谓的互斥就是线程之间互相排斥,获得资源的线程排斥其它没有获得资源的线程。在多线程编程中,需要保证共享数据操作的安全性,引入了互斥锁的概念.每个对象对应一个互斥锁的标记.这个标记用来保证任意时刻只能有一个线程访问改对象。
从互斥锁的这种行为看,线程加锁和解锁之间的代码相当于一个独木桥,同意时刻只有一个线程能执行。从全局上看,在这个地方,所有并行运行的线程都变成了排队运行了。比较专业的叫法是同步执行,这段代码区域叫临界区。同步执行就破坏了线程并行性的初衷了,临界区越大破坏得越厉害。所以在实际应用中,应该尽量避免有临界区出现。实在不行,临界区也要尽量的小。如果连缩小临界区都做不到,那还使用多线程干嘛?

pthread_mutex_t互斥锁

Linux初始化和销毁互斥锁的接口是pthread_mutex_init()和pthead_mutex_destroy(),对于加锁和解锁则有pthread_mutex_lock()、pthread_mutex_trylock()和pthread_mutex_unlock()。这些接口的完整定义如下:

1:
pthread_mutex_init(pthread_mutex_t * mutex, const pthread_mutexattr_t *attr);
// 初始化锁变量mutex。
// attr为锁属性,NULL值为默认属性。

2:
pthread_mutex_lock(pthread_mutex_t *mutex);
// 加锁(阻塞操作)

3:
pthread_mutex_trylock(pthread_mutex_t *mutex);
// 试图加锁(不阻塞操作)
// 当互斥锁空闲时将占有该锁;否则立即返回
// 但是与2不一样的是当锁已经在使用的时候,返回为EBUSY,而不是挂起等待。

4:
pthread_mutex_unlock(pthread_mutex_t *mutex);
释放锁

5:pthread_mutex_destroy(pthread_mutex_t *mutex);
使用完后删除

pthread_mutex_t互斥锁案例

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

pthread_mutex_t g_mutex;
int g_lock_var = 0;

void* task1(void *args) {
    int ret;
    time_t end_time;
    end_time = time(NULL) + 10;
    printf("%s", "aaaaaaaa\n");
    while (time(NULL) < end_time) {

        ret = pthread_mutex_trylock(&g_mutex);
        if ( EBUSY == ret) {
            printf("thread1: the varible is locked by thread2.\n");
        } else {
            printf("thread1: lock the varialbe! \n");
            ++g_lock_var;
            pthread_mutex_unlock(&g_mutex);
        }
        sleep(1);
    }
    return NULL;
}
void* task2(void *args) {

    time_t end_time;
    end_time = time(NULL) + 10;
    while (time(NULL) < end_time) {
        pthread_mutex_lock(&g_mutex);
        printf("thread2: lock the variale!\n");
        ++g_lock_var;
        sleep(1);
        pthread_mutex_unlock( &g_mutex );
    }
    return NULL;
}

int main() {
    pthread_t  thread1, thread2;
    pthread_mutex_init(&g_mutex, NULL);
    pthread_create(&thread1, NULL, task1, NULL);
    pthread_create(&thread2, NULL, task2, NULL);
    
    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);
    pthread_mutex_destroy(&g_mutex);
    printf("g_lock_var= %d\n", g_lock_var);
    return 0;
}

条件变量保障线程安全性

条件变量关键点在 "变量"上。根据 "条件"来选择是否在那里等待,等待允许通过的信号.
这个信号的控制是由另外一个线程来控制的。
初始化和销毁条件变量的接口是pthread_cond_init()和pthread_cond_destory();控制“事件”发生的接口是pthread_cond_signal()或pthread_cond_broadcast();等待“事件”发生的接口是pthead_cond_wait()或pthread_cond_timedwait()。
他们的完整定义如下:


int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr);  
int pthread_cond_destory(pthread_cond_t *cond);  
//
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); 
// 等待会无限期的等待
int pthread_cond_timedwait(pthread_cond_t *cond,pthread_mutex_t *mutex, const timespec *abstime); 
// 限时的等待,等待一定时间如果"事件"依然没有发生则去做别的事情。
int pthread_cond_signal(pthread_cond_t *cond);  
// 单播只有一个线程会得到 已经发生了的通知
int pthread_cond_broadcast(pthread_cond_t *cond);  
// 广播所有线程都会得到事件通知的线程,所有也需要经过互斥锁控制的独木桥

条件变量 使用案例

//
// Created by 汪庭东 on 2020/4/12.
//
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

#define BUFFER_SIZE 5
pthread_mutex_t g_mutex;
pthread_cond_t g_cond;
typedef struct {
    char buf[BUFFER_SIZE];
    int count;
}buffer_t;
buffer_t g_share = {"", 0};
char g_ch = 'A';
void* producer(void *args) {
    printf("producer start .....%d\n", g_share.count);

    while (g_ch < 'Z') {
        pthread_mutex_lock(&g_mutex);
        printf("producer get lock...\n");

        if (g_share.count < BUFFER_SIZE) {
            g_share.buf[g_share.count++] = g_ch++;
            printf("produer got char [%c]\n", g_ch - 1);
            if (g_share.count == BUFFER_SIZE) {
                printf("Producer singaling full. \n");
                pthread_cond_signal(&g_cond);
            }
        }


        sleep(1);
        pthread_mutex_unlock(&g_mutex);
    }
    printf("producer exit.\n");
}

void* consumer(void *args) {
    printf("consumer start ......\n");
    while (g_ch < 'Z') {
        pthread_mutex_lock(&g_mutex);
        printf("consumer get lock .... \n");
        printf("consumer cond wait .....\n");
        pthread_cond_wait(&g_cond, &g_mutex);
        printf("consumer condition wait get ....%d\n", g_share.count);
        for (int i = 0; g_share.buf[i] && g_share.count; ++i) {
            putchar(g_share.buf[i]);
            --g_share.count;
            sleep(1);
        }
        putchar("\n");
        pthread_mutex_unlock(&g_mutex);
    }
    printf("consumer exit.");
    return NULL;
}
int main() {
    pthread_t producer_thread;
    pthread_t consumer_thread;
    pthread_mutex_init(&g_mutex, NULL);
    pthread_cond_init(&g_cond, NULL);
    pthread_create(&producer_thread, NULL, producer, NULL);
    pthread_create(&consumer_thread, NULL, consumer, NULL);
    pthread_join(producer_thread, NULL);
    pthread_join(consumer_thread, NULL);
    pthread_cond_destroy(&g_cond);
    pthread_mutex_destroy(&g_mutex);
    return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • Q:为什么出现多线程? A:为了实现同时干多件事的需求(并发),同时进行着下载和页面UI刷新。对于处理器,为每个线...
    幸福相依阅读 1,562评论 0 2
  • linux编程-线程 MUTEX 一.概述 互斥量是线程同步的一...
    Aska偶阵雨阅读 472评论 0 0
  • 线程基础 线程是进程的一个执行单元,执行一段程序片段,线程共享全局变量;线程的查看可以使用命令或者文件来进行查看;...
    秋风弄影阅读 727评论 0 0
  • 本篇主要讲Linux环境下的多线程同步内核对象。 (1)linux线程同步之互斥体:linux互斥体的用法与win...
    菠落箩落萝阅读 620评论 0 0
  • OSSpinLock OSSpinLock 不再安全,主要原因发生在低优先级线程拿到锁时,高优先级线程进入忙等(b...
    GAME666阅读 1,276评论 0 0