文章目录

  • 一、mutex 基本函数
    • 1. `int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);`
    • 2. `int pthread_mutex_lock(pthread_mutex_t *mutex);`
    • 3. `int pthread_mutex_trylock(pthread_mutex_t *mutex);`
    • 4. `int pthread_mutex_unlock(pthread_mutex_t *mutex);`
    • 5. `int pthread_mutex_destory(pthread_mutex_t *mutex);`
  • 二、mutex 多线程
    • 1. 多线程没有引入 mutex
    • 2. 多线程引入 mutex

一、mutex 基本函数

1. int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr);

可使用宏静态初始化互斥量,如下:等价于用NULL指定 attr 调用 pthread_mutex_init 函数

pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;

2. int pthread_mutex_lock(pthread_mutex_t *mutex);

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

3. int pthread_mutex_trylock(pthread_mutex_t *mutex);

非阻塞上锁,如果互斥量处于未锁住状态,那么将锁住互斥量,否则立即返回失败 EBUSY

4. int pthread_mutex_unlock(pthread_mutex_t *mutex);

释放锁

5. int pthread_mutex_destory(pthread_mutex_t *mutex);

用于销毁互斥量,释放所有相关联的资源(由 pthread_mutex_init自动申请的资源)

二、mutex 多线程

1. 多线程没有引入 mutex

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>void printer(const char *str)
{while(*str!='\0'){putchar(*str);fflush(stdout);str++;sleep(1);}printf("\n");
}void *thread_fun_1(void *arg)
{const char *str = "hello";printer(str);
}void *thread_fun_2(void *arg)
{const char *str = "world";printer(str);
}int main(void)
{pthread_t tid1, tid2;pthread_create(&tid1, NULL, thread_fun_1, NULL);pthread_create(&tid2, NULL, thread_fun_2, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);return 0;
}

2. 多线程引入 mutex

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex;void printer(const char *str)
{pthread_mutex_lock(&mutex);while(*str!='\0'){putchar(*str);fflush(stdout);str++;sleep(1);}printf("\n");pthread_mutex_unlock(&mutex);
}void *thread_fun_1(void *arg)
{const char *str = "hello";printer(str);
}void *thread_fun_2(void *arg)
{const char *str = "world";printer(str);
}int main(void)
{pthread_t tid1, tid2;pthread_mutex_init(&mutex, NULL);pthread_create(&tid1, NULL, thread_fun_1, NULL);pthread_create(&tid2, NULL, thread_fun_2, NULL);pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_mutex_destroy(&mutex);return 0;
}

mutex 使用介绍相关推荐

  1. android 线程互斥锁,线程锁(互斥锁Mutex)及递归锁

    一.线程锁(互斥锁) 在一个程序内,主进程可以启动很多个线程,这些线程都可以访问主进程的内存空间,在Python中虽然有了GIL,同一时间只有一个线程在运行,可是这些线程的调度都归系统,操作系统有自身 ...

  2. 多线程原理分析面试题理解

    系列前言 本系列是本人参加微软亚洲研究院,腾讯研究院,迅雷面试时整理的,另外也加入一些其它IT公司如百度,阿里巴巴的笔试面试题目,因此具有很强的针对性.系列中不但会详细讲解多线程同步互斥的各种&quo ...

  3. 多线程下的进程同步(线程同步问题总结篇)

    之前写过两篇关于线程同步问题的文章(一,二),这篇中将对相关话题进行总结,本文中也对.NET 4.0中新增的一些同步机制进行了介绍. 首先需要说明的是为什么需要线程功能同步.MSDN中有这样一段话很好 ...

  4. C++11 timed_mutex

    目录 一 timed_mutex 二 try_lock_for 三 try_lock_until 四 recursive_timed_mutex 五 参考 一 timed_mutex 前文C++11 ...

  5. 多线程间的通信和同步

    最近看了很多关于网络编程和多线程的书,为了以后查看相关内容方便,整理了几本书的精华形成这篇博文,希望能帮助观看这篇博文的读者. 目录 一.什么是多线程? 二.为什么要创建线程 三.线程之间如何通信 四 ...

  6. 转:AbstractQueuedSynchronizer的介绍和原理分析

    引自:http://ifeve.com/introduce-abstractqueuedsynchronizer/ 简介 提供了一个基于FIFO队列,可以用于构建锁或者其他相关同步装置的基础框架.该同 ...

  7. libevent介绍

    libevent是一款事件驱动的网络开发包 由于采用 c 语言开发 体积小巧,跨平台,速度极快. 通常我们在建立服务器的处理模型的时候,主要是下面集中模型; (1)    a new Connecti ...

  8. Linux下多线程编程中信号量介绍及简单使用

    在Linux中有两种方法用于处理线程同步:信号量和互斥量. 线程的信号量是一种特殊的变量,它可以被增加或减少,但对其的关键访问被保证是原子操作.如果一个程序中有多个线程试图改变一个信号量的值,系统将保 ...

  9. 改进型 clock 页面置换算法实现_ID生成算法雪花算法介绍及实现

    1. SnowFlake 算法介绍 雪花算法是由 Twitter 公司开源的可在分布式系统中产生一个全局唯一 ID 的算法.最初 Twitter 把存储系统从 MySQL 迁移到 Cassandra, ...

最新文章

  1. 皮一皮:好的产品营销该怎么学习?看这...
  2. 基于ACE Proactor框架下高并发、大容量吞吐程序设计既最近的一个产品开发总结
  3. JMeter初探五-配置元件与参数化
  4. 赛程一览 | 2019 上海国际创客大赛
  5. 【规范】前端编码规范——css 规范
  6. HDLBits答案(20)_Verilog有限状态机(7)
  7. NSNotification、delegate和KVO的区别
  8. 开源软件和自由软件_自由和开源软件的经济学
  9. 这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧
  10. nginx源码分析(2)——http模块的初始化过程
  11. coreboot学习2:项目源码的初步了解
  12. Perl用LWP实现GET/POST数据发送 原
  13. JSonCpp库使用
  14. python中printf的用法_python输出语句print的用法是什么?
  15. 数论中的偶数阶Abel群的阶
  16. ER-X刷回原版固件方法(救砖)
  17. 知识图谱-实体消歧(语义消歧)
  18. c语言数字大小排序的理解,教孩子数字比大小,排序很重要
  19. 高盛vr/ar研究报告
  20. 怎么分析出京东快递物流多次派件的单号

热门文章

  1. [vue-router] Failed to resolve async component render: TypeError: Cannot read properties of undefine
  2. Java并发包源码学习系列:LBD双端阻塞队列源码解析
  3. ARFoundation入门到精通 - 1.3 ARkit 支持机型
  4. Windows XP的20个实用技巧
  5. c语言 n阶阶乘尾0个数,计算n的阶乘(n!)末尾0的个数
  6. 通达OA 一次升级引发的即时通讯工具不能接收离线信息的血案
  7. 小学学校计算机生均比是多少,义务教育基本办学条件十项指标.doc
  8. XTU 1102海明距离
  9. python的wxpy库_1、初学探讨PYTHON的itchat和wxpy两库
  10. Agile and ASPICE 系列 - project management planning