linux进程线程同步之 - POSIX线程互斥锁

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,同步,互斥锁)