当前位置: 首页 > news >正文

现代C++多线程基础 -忆苦思甜pthread_mutex

c 老古董

文章目录

  • c 老古董
    • pthread_mutex
      • 概念
      • 常用api
          • pthread_mutex_init
          • pthread_mutex_lock
          • pthread_mutex_trylock
          • `pthread_mutex_unlock`
          • `pthread_mutex_destroy`
      • 案例

pthread_mutex

概念

互斥锁 mutex是一种简单的加锁的方法来控制对共享资源的访问,mutex只有两种状态,即

上锁(lock)

解锁(unlock)。

在访问该资源前,首先应申请mutex,

  • 如果mutex处于lock状态,则默认阻塞申请者。

  • 如果mutex处于unlock状态,则会申请到mutex并立即lock;

    unlock操作应该由lock者进行

常用api

pthread_mutex_init
  • 静态分配
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  • 动态分配
int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutexattr_t *attr);pthread_mutex_t mutex;pthread_mutex_init(&mutex, NULL);

功能

  • 初始化一个互斥锁。

参数:

  • mutex:指定的互斥锁 互斥锁地址。
  • attr:互斥锁的属性,NULL 为默认的属性。

返回值:

  • 成功返回 0,失败返回非 0。
pthread_mutex_lock
#include <pthread.h> int pthread_mutex_lock(pthread_mutex_t *mutex); 

功能:

  • 对互斥锁上锁,
  • 若已经上锁,则调用者一直阻塞到互斥锁解锁

参数:

  • mutex:指定的互斥锁 互斥锁地址。

返回值:

  • 成功返回 0,失败返回非 0。
pthread_mutex_trylock
#include <pthread.h> int pthread_mutex_trylock(pthread_mutex_t *mutex);

功能:

  • 对互斥锁上锁,
  • 若已经上锁,则上锁失败,函数立即返回。

参数:

  • mutex:指定的互斥锁 互斥锁地址。

返回值:

  • 成功返回 0,失败返回非 0。
pthread_mutex_unlock
#include <pthread.h>int pthread_mutex_unlock(pthread_mutex_t * mutex);

功能:

  • 对指定的互斥锁解锁。

参数:

  • mutex:互斥锁地址。

返回值:

  • 成功返回 0,失败返回非 0。
pthread_mutex_destroy

在所有使用过此互斥锁的线程都不再需要使用时候,应调用pthread_mutex_destroy销毁互斥锁

pthread_mutex_t mymutex;    
pthread_mutex_init(&mymutex, NULL);// 当互斥锁使用完毕后,要销毁
pthread_mutex_destroy(&mymutex);

案例

两人公用同一银行账户。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>//通过互斥锁解决线程间互斥问题int money = 10000;//第一步:创建互斥锁(由于两个线程操作同一个互斥锁,所以定义在全局更加方便一点)
pthread_mutex_t mymutex;void *pthread_fun1(void *arg)
{int get, rest, actual;get = 10000;//第三步:对共享资源的操作进行上锁pthread_mutex_lock(&mymutex);printf("张三正在查询余额...\n");sleep(1);rest = money;printf("张三正在取钱...\n");sleep(1);if(get > rest){actual = 0;}else {actual = get;rest = rest - get;money = rest;}printf("张三想取%d元,实际取了%d元,余额为%d元\n", get, actual, rest);//第四步:当共享资源的操作执行完毕后,对互斥锁执行解锁操作pthread_mutex_unlock(&mymutex);pthread_exit(NULL);
}void *pthread_fun2(void *arg)
{int get, rest, actual;get = 10000;//第三步:对共享资源的操作进行上锁pthread_mutex_lock(&mymutex);printf("李四正在查询余额...\n");sleep(1);rest = money;printf("李四正在取钱...\n");sleep(1);if(get > yu){actual = 0;}else {actual = get;rest = rest - get;money = rest;}printf("李四想取%d元,实际取了%d元,余额为%d元\n", get, actual, rest);//第四步:当共享资源的操作执行完毕后,对互斥锁执行解锁操作pthread_mutex_unlock(&mymutex);pthread_exit(NULL);
}int main(int argc, char const *argv[])
{//第二步:初始化互斥锁pthread_mutex_init(&mymutex, NULL);pthread_t thread1, thread2;if(pthread_create(&thread1, NULL, pthread_fun1, NULL) != 0){perror("fail to pthread_create");exit(1);}if(pthread_create(&thread2, NULL, pthread_fun2, NULL) != 0){perror("fail to pthread_create");exit(1);}pthread_join(thread1, NULL);pthread_join(thread2, NULL);//第五步:当互斥锁使用完毕后,要销毁pthread_mutex_destroy(&mymutex);return 0;
}
http://www.lryc.cn/news/534970.html

相关文章:

  • soular基础教程-使用指南
  • 网络安全网格架构(CSMA) 网络安全框架csf
  • 基于DeepSeek API和VSCode的自动化网页生成流程
  • 【AI时代】Page Assist - 本地 AI 模型的 Web UI (谷歌浏览器) 本地DeepSeek启用联网功能
  • 电脑IP地址自定义
  • python卷积神经网络人脸识别示例实现详解
  • EX_25/2/11
  • 二.2 整数表示(2.1-2.4)
  • 中间件-安装Minio-集成使用(ubantu-docker)
  • 夸克网盘多链接批量保存,自动同步更新,批量分享
  • 2025清华:DeepSeek从入门到精通.pdf(附下载)
  • 【AIGC】在VSCode中集成 DeepSeek(OPEN AI同理)
  • android动态设置是否允许应用卸载
  • 基于微信小程序的博物馆预约系统的设计与实现
  • 使用NPOI自定义导出excel文件
  • 基于vue2 的 vueDraggable 示例,包括组件区、组件放置区、组件参数设置区 在同一个文件中实现
  • 使用rknn进行facenet部署
  • #渗透测试#批量漏洞挖掘#29网课交单平台 SQL注入
  • 百问网imx6ullpro调试记录(linux+qt)
  • 【python】3_容器
  • 数据结构与算法:动态规划dp:背包问题:理论基础(状态压缩/滚动数组)和相关力扣题(416. 分割等和子集、1049.最后一块石头的重量Ⅱ、494.目标和)
  • 数字游牧时代:IT人力外包的范式革命与文明重构
  • Qt - 地图相关 —— 3、Qt调用高德在线地图功能示例(附源码)
  • cloudberry测试
  • RocketMQ、RabbitMQ、Kafka 的底层实现、功能异同、应用场景及技术选型分析
  • UWB功耗大数据插桩调研
  • 郭羽冲IOI2024参赛总结
  • 03:Spring之Web
  • lx-music落雪音乐-开源免费听歌软件[提供最新音源使用, 支持全网平台, 支持无损音乐下载]
  • 129,【2】buuctf [BJDCTF2020]EzPHP