POSIX线程互斥锁

使用范围:线程同步

本文转自:http://blog.csdn.net/jiebaoabcabc/article/details/37914769

一、函数介绍

1.初始化互斥锁

#include <pthread.h>

int pthread_mutex_init(pthread_mutex_t*restrict mutex,const pthread_mutexattr_t *restrict attr);

函数功能:根据输入的参数和配置初始化线程互斥锁。

返回值:If successful, the pthread_mutex_init() functions shallreturn zero; otherwise, an error number shall be returned to indicate the error.

错误:

The pthread_mutex_init() function shall fail if:

EAGAIN

The system lacked the necessary resources(other than memory) to initialize another mutex.

ENOMEM

Insufficient memory exists to initializethe mutex.

EPERM

The caller does not have the privilege toperform the operation.

The pthread_mutex_init() function may fail if:

EBUSY

The implementation has detected an attemptto reinitialize the object referenced by mutex, a previously initialized, but not yetdestroyed, mutex.

EINVAL

The value specified by attr is invalid.

These functions shall not return an errorcode of [EINTR].

输入参数:1.mutex

将需要被初始化的pthread_mutex_t类型的变量指针传递给互斥锁初始化函数,互斥锁初始化函数将其动态初始化。

2.attr

互斥锁初始化属性配置输入,如果传入为空,则使用默认的互斥锁属性,默认使用PTHREAD_MUTEX_DEFAULT。

合法的类型属性值有:

PTHREAD_MUTEX_NORMAL;

PTHREAD_MUTEX_ERRORCHECK;

PTHREAD_MUTEX_RECURSIVE;

PTHREAD_MUTEX_DEFAULT。

类型说明:

PTHREAD_MUTEX_NORMAL   快速互斥锁

这种类型的互斥锁不会自动检测死锁。如果一个线程试图对一个互斥锁重复锁定,将会引起这个线程的死锁。如果试图解锁一个由别的线程锁定的互斥锁会引发不可预料的结果。如果一个线程试图解锁已经被解锁的互斥锁也会引发不可预料的结果。

PTHREAD_MUTEX_ERRORCHECK  检错互斥锁

这种类型的互斥锁会自动检测死锁。如果一个线程试图对一个互斥锁重复锁定,将会返回一个错误代码。如果试图解锁一个由别的线程锁定的互斥锁将会返回一个错误代码。如果一个线程试图解锁已经被解锁的互斥锁也将会返回一个错误代码。

PTHREAD_MUTEX_RECURSIVE  递归互斥锁

如果一个线程对这种类型的互斥锁重复上锁,不会引起死锁,一个线程对这类互斥锁的多次重复上锁必须由这个线程来重复相同数量的解锁,这样才能解开这个互斥锁,别的线程才能得到这个互斥锁。如果试图解锁一个由别的线程锁定的互斥锁将会返回一个错误代码。如果一个线程试图解锁已经被解锁的互斥锁也将会返回一个错误代码。这种类型的互斥锁只能是进程私有的(作用域属性为PTHREAD_PROCESS_PRIVATE)。

PTHREAD_MUTEX_DEFAULT   快速互斥锁

这种类型的互斥锁不会自动检测死锁。如果一个线程试图对一个互斥锁重复锁定,将会引起不可预料的结果。如果试图解锁一个由别的线程锁定的互斥锁会引发不可预料的结果。如果一个线程试图解锁已经被解锁的互斥锁也会引发不可预料的结果。POSIX标准规定,对于某一具体的实现,可以把这种类型的互斥锁定义为其他类型的互斥锁。

互斥锁可以分为快速互斥锁、递归互斥锁和检错互斥锁。这三种锁的区别主要在于其他未占有互斥锁的线程在希望得到互斥锁时的是否需要阻塞等待。快速锁是指调用线程会阻塞直至拥有互斥锁的线程解锁为止。递归互斥锁能够成功地返回并且增加调用线程在互斥上加锁的次数,而检错互斥锁则为快速互斥锁的非阻塞版本,它会立即返回并返回一个错误信息。

2.销毁互斥锁

#include <pthread.h>

int pthread_mutex_destroy(pthread_mutex_t *mutex);

函数功能:通过传入的mutex参数销毁互斥锁

返回值:If successful, the pthread_mutex_destroy() functions shallreturn zero; otherwise, an error number shall be returned to indicate the error.

错误:EBUSY

The implementation has detected an attemptto destroy the object referenced by mutex while it is locked or referenced (forexample, while being used in apthread_cond_timedwait() or pthread_cond_wait()) by another thread.

          EINVAL

The value specified by mutex is invalid.

输入参数:mutex

指向需要销毁的互斥锁结构的指针。

3.上锁互斥锁

#include <pthread.h>

int pthread_mutex_lock(pthread_mutex_t *mutex);

函数功能:如果已经锁定了互斥对象,调用线程将阻塞,直到互斥锁解锁

返回值:If successful, the pthread_mutex_lock() functions shallreturn zero; otherwise, an error number shall be returned to indicate the error.

错误:EINVAL

The mutex was created with the protocol attributehaving the value PTHREAD_PRIO_PROTECT and the calling thread's priority ishigher than the mutex's current priority ceiling.

           EBUSY

The mutex could not be acquired because it wasalready locked.

        EDEADLK

The current thread already owns the mutex.

输入参数:mutex

指向需要获取互斥锁并给其上锁的结构的指针。

4.试图上锁互斥锁

#include <pthread.h>

int pthread_mutex_trylock(pthread_mutex_t *mutex);

函数功能:试图根据mutex为其上互斥锁,如果其已经被上锁,则不会阻塞线程。

返回值:The pthread_mutex_trylock() function shallreturn zero if a lock on the mutex object referenced by mutex isacquired. Otherwise, an error number is returned to indicate the error.

错误:EINVAL

The value specified by mutex does not refer to an initialized mutexobject.

           EAGAIN

The mutex could not be acquired becausethe maximum number of recursive locks for mutex has been exceeded.

          EBUSY

The mutex could not be acquired because it wasalready locked.

输入参数:mutex

指向需要获取互斥锁并给其上锁的结构的指针。

5.带定时上锁互斥锁

#include <pthread.h>
#include <time.h>

int pthread_mutex_timedlock(pthread_mutex_t*restrict mutex,
const structtimespec *restrict abs_timeout); 

函数功能:如果已经锁定了互斥对象,调用线程将阻塞,直到互斥锁解锁,或者定时时间到。

返回值:If successful, the pthread_mutex_timedlock() function shallreturn zero; otherwise, an error number shall be returned to indicate the error.

错误:EINVAL

The mutex was created with the protocolattribute having the value PTHREAD_PRIO_PROTECT and the calling thread'spriority is higher than the mutex' current priority ceiling.

          EINVAL

The process or thread would have blocked,and the abs_timeout parameter specified a nanoseconds fieldvalue less than zero or greater than or equal to 1000 million.

          ETIMEDOUT

The mutex could not be locked before thespecified timeout expired.

          EAGAIN

The mutex could not be acquired becausethe maximum number of recursive locks for mutex has been exceeded.

          EDEADLK

The current thread already owns the mutex.

输入参数:1.mutex

指向需要获取互斥锁并给其上锁的结构的指针。

2. abs_timeout

定时时间设置。

6.解锁互斥锁

#include <pthread.h>

int pthread_mutex_unlock(pthread_mutex_t *mutex);

函数功能:根据mutex解锁互斥锁

返回值:If successful, the pthread_mutex_unlock() functions shallreturn zero; otherwise, an error number shall be returned to indicate the error.

错误:EINVAL

The value specified by mutex does not refer to an initialized mutexobject.

           EAGAIN

The mutex could not be acquired becausethe maximum number of recursive locks for mutex has been exceeded.

        EPERM

The current thread does not own the mutex.

输入参数:mutex

指向需要释放互斥锁并给其解锁的结构的指针。

二、常用结构

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

pthread_mutex_tmutex;

pthread_mutex_init(&mutex,NULL);

pthread_mutex_lock(&mutex);

//---------------------------------------↓临界区↓----------------------------------------------

/* dosomething */

//---------------------------------------↑临界区↑----------------------------------------------

pthread_mutex_unlock(&mutex);

pthread_mutex_destroy(&mutex);

linux进程线程同步之 - POSIX线程互斥锁相关推荐

  1. 【转载】同步和互斥的POSIX支持(互斥锁,条件变量,自旋锁)

    上篇文章也蛮好,线程同步之条件变量与互斥锁的结合: http://www.cnblogs.com/charlesblc/p/6143397.html  现在有这篇文章: http://blog.csd ...

  2. java线程 同步与异步 线程池

    1)多线程并发时,多个线程同时请求同一个资源,必然导致此资源的数据不安全,A线程修改了B线 程的处理的数据,而B线程又修改了A线程处理的数理.显然这是由于全局资源造成的,有时为了解 决此问题,优先考虑 ...

  3. (删)Java线程同步实现二:Lock锁和Condition

    在上篇文章(3.Java多线程总结系列:Java的线程同步实现)中,我们介绍了用synchronized关键字实现线程同步.但在Java中还有一种方式可以实现线程同步,那就是Lock锁. 一.同步锁 ...

  4. 线程安全,线程同步,解决线程同步问题

    一.线程安全问题 如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样 的,而且其他的变量的值也和预期的是一样的,就是线程安全的. 简单来说就是多线程访 ...

  5. java线程同步——竞争条件的荔枝+锁对象

    [0]README 0.1) 本文描述转自 core java volume 1, 源代码为原创,旨在理解 java线程同步--竞争条件的荔枝+锁对象 的相关知识: 0.2) for full sou ...

  6. Linux线程同步与Windows线程同步

    简介 线程同步概念:在多线程下,在一段时间内只允许一个线程访问资源,不允许其它线程访问. 在WIN32中,同步机制主要有以下几种: (1)事件(Event); (2)信号量(semaphore); ( ...

  7. 【Windows】线程漫谈——线程同步之信号量和互斥量

    本系列意在记录Windwos线程的相关知识点,包括线程基础.线程调度.线程同步.TLS.线程池等 信号量内核对象 信号量内核对象用来进行资源计数,它包含一个使用计数.最大资源数.当前资源计数.最大资源 ...

  8. VC++中多线程学习(MFC多线程)三(线程同步包含:原子互锁、关键代码段、互斥器Mutex、Semaphores(信号量)、Event Objects(事件))

    目录 ​​​​​​​​​​​​ 线程同步的必要性: 2.解决同步问题的方法 2.1原子互锁家族函数 2.2Critical Sections(关键代码段.关键区域.临界区域) 2.3 互斥器Mutex ...

  9. 线程同步(临界区、互斥量、事件、信号量)

    1.为什么线程要同步? #include<windows.h> #include<iostream> using namespace std; DWORD WINAPI Thr ...

最新文章

  1. 遇到的问题及解决方法
  2. Asp.net2.0 学习资源(转载)
  3. 前端学习(1317):静态资源2
  4. SAP中的“定单状态”
  5. 深度相机---(2)结构光深度测距
  6. 安装指定版本的GPU版本的tensorflow小技巧
  7. C语言:编写一个程序,从键盘读入一个矩形的两个边的值(整数),求矩形面积
  8. DotNetNuke中的函数式编程手法分析
  9. 重载下标操作符,检查内存越界
  10. IIS安装和ASP.NET Web应用程序开发期间部署到IIS自定义主机域名并附加进程调试...
  11. 酒店管理系统c语言课程设计,基于C语言的酒店管理系统课程设计.doc
  12. PDF 文件如何转换从可以编辑的文本和word
  13. BUUCTF Cipher writeup
  14. 基于OMNET++仿真平台的4维超立方体网络仿真
  15. 二进制换算成八进制、十进制、十六进制
  16. 益寿延年,这13种食物真是宝,能延寿10年,赶紧收藏!
  17. XSS(Cross Site Scripting)攻击简介
  18. 计算机网络——(6)网络互联技术与设备
  19. Python入门记录(6)输入三次密码
  20. html 做笔记,HTML笔记(一)

热门文章

  1. 穿越晋商百年-体验非遗文化
  2. 江苏28岁美女法医2年内检验400具尸体(图)
  3. 计算机视觉基础知识(第一章)
  4. R 语言怎么保存工作目录到当前路径_第一讲 R基本介绍及安装
  5. 指针型函数实现插入字符
  6. 助力高校科研信息化升级,让高校更“高效”
  7. MATLAB求最大特征值和特征向量
  8. 528全国爱发日,你的头发还好么,防脱秘籍送给你!
  9. 计算机网络,c语言,电子
  10. JSON格式字符串解析,转换为JSONObject