Linux线程同步-互斥锁

这是学习C语言中文网的笔记

简介

互斥锁(Mutex)又称互斥量或者互斥体,是最简单也最有效地一种线程同步机制。互斥锁的用法和实际生活中的锁非常类似,当一个线程访问公共资源时,会及时地“锁上”该资源,阻止其它线程访问;访问结束后再进行“解锁”操作,将该资源让给其它线程访问。

函数声明

pthread_mutex_t lock; /* 实例化互斥锁结构体 */
pthread_mutex_init(&lock, NULL); /* 动态初始化,	成功返回0,失败返回非0 */
pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER; /* 静态初始化 */
pthread_mutex_lock(&lock); /* 阻塞的锁定互斥锁 */
pthread_mutex_trylock(&thread_mutex)/* 非阻塞的锁定互斥锁,成功获得互斥锁返回0,如果未能获得互斥锁,立即返回一个错误码 */
pthread_mutex_unlock(&lock)/* 解锁互斥锁 */
pthread_mutex_destroy(&lock) /* 销毁互斥锁 */

不加锁的代码实例(Linux线程同步详解

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


int ticket_sum = 10;

void *sell_ticket(void *arg){
    int i;
    for ( i = 0; i < 10; i++)
    {
        if(ticket_sum >0)
        {
            sleep(1);

            printf("%u 卖第 %d 张票\n", (unsigned int)pthread_self(), 10 - ticket_sum +1 );
            ticket_sum --;
        }
    }

    return 0;
}

int main(){
    int flag, i ;
    void *ans;

    pthread_t tids[4];

    
    for ( i = 0; i < 4; i++)
    {
        flag = pthread_create(&tids[i], NULL, &sell_ticket, NULL);
        if (flag != 0)
        {
            printf("creat thread fail!!");
            return 0;
        }
    }
    
    sleep(100);

    for ( i = 0; i < 4; i++)
    {
        flag = pthread_join(tids[i], &ans);
        if (flag != 0){
            printf("tid=%d join fail !!" , (int)tids[i]);
            return 0;
        }
    }

    return 0;
}

编译运行

root@192:/usr/local/workspace# gcc -o thread thread.c -lpthread

root@192:/usr/local/workspace# ./thread 

输出结果

root@192:/usr/local/workspace# gcc -o thread thread.c -lpthread
thread.c: In function ‘sell_ticket’:
thread.c:14:13: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
             sleep(1);
             ^
root@192:/usr/local/workspace# ./thread 
2840495872 卖第 1 张票
2848888576 卖第 2 张票
2865673984 卖第 3 张票
2857281280 卖第 4 张票
2840495872 卖第 5 张票
2857281280 卖第 5 张票
2848888576 卖第 5 张票
2865673984 卖第 5 张票
2840495872 卖第 9 张票
2857281280 卖第 9 张票
2848888576 卖第 9 张票
2865673984 卖第 9 张票
2840495872 卖第 13 张票

加上互斥锁后

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

pthread_mutex_t lock;
int ticket_sum = 10;

void *sell_ticket(void *arg){
    int i;
    for ( i = 0; i < 10; i++)
    {
        pthread_mutex_lock(&lock);
        if(ticket_sum >0)
        {
            sleep(1);

            printf("%u 卖第 %d 张票\n", (unsigned int)pthread_self(), 10 - ticket_sum +1 );
            ticket_sum --;
        }
        pthread_mutex_unlock(&lock);
    }

    return 0;
}

int main(){
    int flag, i ;
    void *ans;

    pthread_t tids[4];

    

    pthread_mutex_init(&lock,NULL);

    for ( i = 0; i < 4; i++)
    {
        flag = pthread_create(&tids[i], NULL, &sell_ticket, NULL);
        if (flag != 0)
        {
            printf("creat thread fail!!");
            return 0;
        }
    }
    
    sleep(100);

    for ( i = 0; i < 4; i++)
    {
        flag = pthread_join(tids[i], &ans);
        if (flag != 0){
            printf("tid=%d join fail !!" , (int)tids[i]);
            return 0;
        }
    }

    pthread_mutex_destroy(&lock);

    return 0;
}

编译运行

root@192:/usr/local/workspace# gcc -o thread thread.c -lpthread

root@192:/usr/local/workspace# ./thread 

输出结果

3386459904 卖第 1 张票
3386459904 卖第 2 张票
3386459904 卖第 3 张票
3386459904 卖第 4 张票
3386459904 卖第 5 张票
3386459904 卖第 6 张票
3386459904 卖第 7 张票
3386459904 卖第 8 张票
3386459904 卖第 9 张票
3386459904 卖第 10 张票
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值