Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5786897
  • 博文数量: 409
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 8273
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-23 19:15
个人简介

qq:78080458 学习交流群:150633458

文章分类

全部博文(409)

文章存档

2019年(127)

2018年(130)

2016年(20)

2015年(60)

2014年(41)

2013年(31)

分类: 嵌入式

2015-05-12 17:31:55

一、互斥量的属性
    就像线程有属性一样,线程的同步互斥量也有属性,比较重要的是进程共享属性和类型属性。互斥量的属性用pthread_mutexattr_t类型的数据
    表示,当然在使用之前必须进行初始化,使用完成之后需要进行销毁:
    1)、互斥量初始化
    int pthread_mutexattr_init(pthread_mutexattr_t *attr);
    2)、互斥量销毁
     int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);

二、互斥量的进程共享属性
    进程共享属性有两种值:
    1)、PTHREAD_PROCESS_PRIVATE,这个是默认值,同一个进程中的多个线程访问同一个同步对象
    2)、PTHREAD_PROCESS_SHARED, 这个属性可以使互斥量在多个进程中进行同步,如果互斥量在多进程的共享内存区域,那么具有这个属性的
            互斥量可以同步多进程

    设置互斥量进程共享属性
      int pthread_mutexattr_getpshared(const pthread_mutexattr_t *restrict attr, int *restrict pshared);
      int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
 
    进程共享属性需要检测系统是否支持,可以检测宏_POSIX_THREAD_PROCESS_SHARED

三、互斥量的类型属性
    类型属性
    互斥量类型                             没有解锁时再次加锁     不占用是解锁          已解锁时解锁
    PTHREAD_MUTEX_NORMAL                 死锁                 未定义                       未定义
    PTHREAD_MUTEX_ERRORCHEK            返回错误          返回错误                    返回错误
    PTHREAD_MUTEX_RECURSIVE             允许                 返回错误                    返回错误
    PTHREAD_MUTEX_DEFAULT                未定义              未定义                       未定义
    获取/设置互斥量的类型属性
       int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type);
       int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);

四、读写锁与条件变量的属性
    1、读写锁也有属性,它只有一个进程共享属性
        读写锁属性初始化
           int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
           int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
        设置读写锁进程共享属性
           int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *restrict attr, int *restrict pshared);
           int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *attr, int pshared); 
    2、条件变量也有进程共享属性
        条件变量属性初始化
           int pthread_condattr_destroy(pthread_condattr_t *attr);
           int pthread_condattr_init(pthread_condattr_t *attr);
        设置条件变量属性
           int pthread_condattr_getpshared(const pthread_condattr_t *restrict attr, int *restrict pshared);
           int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared);
 
五、手册
PTHREAD_MUTEXATTR_DESTROY(3POSIX Programmer’s ManPTHREAD_MUTEXATTR_DESTROY(3P)
PROLOG
       This  manual page is part of the POSIX Programmer’s Manual.  The Linux implementation of this interface may differ (con-
       sult the corresponding Linux manual page for details of Linux behavior), or the interface  may  not  be  implemented  on
       Linux.
        //这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口

NAME
       pthread_mutexattr_destroy, pthread_mutexattr_init - destroy and initialize the mutex attributes object
        //初始化或者销毁互斥量的属性

SYNOPSIS
       #include
        //头文件

       int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
       int pthread_mutexattr_init(pthread_mutexattr_t *attr);

DESCRIPTION
       The  pthread_mutexattr_destroy() function shall destroy a mutex attributes object; the object becomes, in effect, unini-
       tialized. An implementation may cause pthread_mutexattr_destroy() to set the object referenced by  attr  to  an  invalid
       value.  A destroyed attr attributes object can be reinitialized using pthread_mutexattr_init(); the results of otherwise
       referencing the object after it has been destroyed are undefined.
        //pthread_mutexattr_destroy()会销毁一个互斥量属性,这个属性将变成一个没有被初始化的值。或者有的实现可能导致
        //互斥量的属性是一个无效的值。一个已经被销户的互斥量属性可以重新被初始化
,使用一个已经被销毁的属性会导致未
        //知的结果

       The pthread_mutexattr_init() function shall initialize a mutex attributes object attr with the default value for all  of
       the attributes defined by the implementation.
        //pthread_mutexattr_init()初始化一个互斥量的属性

       Results are undefined if pthread_mutexattr_init() is called specifying an already initialized attr attributes object.
        //如果初始化一个已经被初始化过的属性,那么结果是未知的

       After  a  mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes
       object (including destruction) shall not affect any previously initialized mutexes.
        //对于已经初始化的属性,不会影响以前的互斥量

RETURN VALUE
       Upon successful completion, pthread_mutexattr_destroy() and pthread_mutexattr_init() shall return  zero;  otherwise,  an
       error number shall be returned to indicate the error.
        //成功返回0,失败返回错误码

ERRORS
       The pthread_mutexattr_destroy() function may fail if:
        //pthread_mutexattr_destroy() 在以下情况失败

       EINVAL The value specified by attr is invalid.
                    //属性值是无效的

       The pthread_mutexattr_init() function shall fail if:
       // pthread_mutexattr_init()会在以下情况失败

       ENOMEM Insufficient memory exists to initialize the mutex attributes object.
                      //没有内存去初始化属性

       These functions shall not return an error code of [EINTR].
        //不会返回EINTR


PTHREAD_MUTEXATTR_GETPSHAREPOSIX Programmer’s PTHREAD_MUTEXATTR_GETPSHARED(3P)
PROLOG
       This  manual page is part of the POSIX Programmer’s Manual.  The Linux implementation of this interface may differ (con-
       sult the corresponding Linux manual page for details of Linux behavior), or the interface  may  not  be  implemented  on
       Linux.
        //这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口

NAME
       pthread_mutexattr_getpshared, pthread_mutexattr_setpshared - get and set the process-shared attribute
        //获取或者设置互斥量的进程共享属性

SYNOPSIS
       #include
        //头文件

       int pthread_mutexattr_getpshared(const pthread_mutexattr_t *  restrict attr, int *restrict pshared);
       int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);


DESCRIPTION
       The  pthread_mutexattr_getpshared()  function shall obtain the value of the process-shared attribute from the attributes
       object referenced by attr. The pthread_mutexattr_setpshared() function shall set the process-shared attribute in an ini-
       tialized attributes object referenced by attr.
        //pthread_mutexattr_getpshared()会获取到互斥量的进程共享属性pthread_mutexattr_setpshared()可以设置互斥量的进程
        //共享属性

       The  process-shared  attribute is set to PTHREAD_PROCESS_SHARED to permit a mutex to be operated upon by any thread that
       has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multi-
       ple  processes.  If  the  process-shared  attribute is PTHREAD_PROCESS_PRIVATE, the mutex shall only be operated upon by
       threads created within the same process as the thread that initialized the mutex;  if  threads  of  differing  processes
       attempt  to operate on such a mutex, the behavior is undefined. The default value of the attribute shall be PTHREAD_PRO-
       CESS_PRIVATE.
        //进程共享属性是PTHREAD_PROCESS_SHARED,这样会允许互斥量在任何不同的线程之间同步数据,甚至这两个线程不在同一个
        //进程里。 如果属性是PTHREAD_PROCESS_PRIVATE,那么互斥量仅仅允许在同一进程范围内同步数据,如果不同进程之间同步数据
        //那么结果是未知的。默认的属性是 PTHREAD_PROCESS_PRIVATE

RETURN VALUE
       Upon successful completion, pthread_mutexattr_setpshared() shall return  zero;  otherwise,  an  error  number  shall  be
       returned to indicate the error.
        //成功返回0,失败返回错误码

       Upon  successful  completion, pthread_mutexattr_getpshared() shall return zero and store the value of the process-shared
       attribute of attr into the object referenced by the pshared parameter. Otherwise, an error number shall be  returned  to
       indicate the error.
        //成功返回0,而且互斥量的进程共享属性被保存起来,否则返回错误码

ERRORS
       The pthread_mutexattr_getpshared() and pthread_mutexattr_setpshared() functions may fail if:
        //在以下情况失败
    
       EINVAL The value specified by attr is invalid.
                    //属性值是无效的

       The pthread_mutexattr_setpshared() function may fail if:
        //pthread_mutexattr_setpshared()会在以下情况失败

       EINVAL The new value specified for the attribute is outside the range of legal values for that attribute.
                    //设置的属性值不在规定范围之内

       These functions shall not return an error code of [EINTR].
        //不会返回EINTR




PTHREAD_MUTEXATTR_GETTYPE(3POSIX Programmer’s ManPTHREAD_MUTEXATTR_GETTYPE(3P)
PROLOG
       This  manual page is part of the POSIX Programmer’s Manual.  The Linux implementation of this interface may differ (con-
       sult the corresponding Linux manual page for details of Linux behavior), or the interface  may  not  be  implemented  on
       Linux.
        //这只是POSIX的手册,Linux对这个接口的实现可能不一样,或者有的根本没有实现这个接口

NAME
       pthread_mutexattr_gettype, pthread_mutexattr_settype - get and set the mutex type attribute
        //获取或者设置互斥量的类型

SYNOPSIS
       #include
        //头文件

       int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr, int *restrict type);
       int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);

DESCRIPTION
       The  pthread_mutexattr_gettype()  and  pthread_mutexattr_settype()  functions, respectively, shall get and set the mutex
       type attribute. This attribute is set in the type parameter to these functions. The default value of the type  attribute
       is PTHREAD_MUTEX_DEFAULT.
        // pthread_mutexattr_gettype()  and  pthread_mutexattr_settype() 获取或者设置互斥量的类型。类型会在参数中设置,默认的
        //属性是PTHREAD_MUTEX_DEFAULT.

       The type of mutex is contained in the type attribute of the mutex attributes. Valid mutex types include:
        //互斥量的类型包含在它的属性中,有效的类型包括:

       PTHREAD_MUTEX_NORMAL
              This  type of mutex does not detect deadlock. A thread attempting to relock this mutex without first unlocking it
              shall deadlock.  Attempting to unlock a mutex locked  by  a  different  thread  results  in  undefined  behavior.
              Attempting to unlock an unlocked mutex results in undefined behavior.
              //这个类型不会检测死锁,当一个线程试图加锁一个还没被解锁的互斥量,那么就可能死锁。试图解锁被其他线程
              //锁住的互斥量,结果是未知的。解锁已经被解锁的互斥量结果是未知的

       PTHREAD_MUTEX_ERRORCHECK
              This  type  of mutex provides error checking. A thread attempting to relock this mutex without first unlocking it
              shall return with an error. A thread attempting to unlock a mutex which another thread has  locked  shall  return
              with an error. A thread attempting to unlock an unlocked mutex shall return with an error.
              //这个类型会提供错误检测。当一个线程试图加锁一个还没被解锁的互斥量,那么就会返回错误码。试图解锁被其他线程
              //锁住的互斥量,返回错误。解锁已经被解锁的互斥量,返回错误

       PTHREAD_MUTEX_RECURSIVE
              A  thread  attempting  to  relock  this  mutex without first unlocking it shall succeed in locking the mutex. The
              relocking deadlock which can occur with mutexes of type PTHREAD_MUTEX_NORMAL  cannot  occur  with  this  type  of
              mutex.  Multiple locks of this mutex shall require the same number of unlocks to release the mutex before another
              thread can acquire the mutex. A thread attempting to unlock a mutex which another thread has locked shall  return
              with an error.  A thread attempting to unlock an unlocked mutex shall return with an error.
              //重新锁住一个未解锁的互斥量会成功。在正常情况下会造成死锁,但是这种类型的却不会。多次锁住互斥量应该有对应的
              //多次解锁操作。试图解锁被其他线程锁住的互斥量,返回错误。解锁已经被解锁的互斥量,返回错误

       PTHREAD_MUTEX_DEFAULT
              Attempting  to  recursively lock a mutex of this type results in undefined behavior. Attempting to unlock a mutex
              of this type which was not locked by the calling thread results in undefined behavior.  Attempting  to  unlock  a
              mutex of this type which is not locked results in undefined behavior. An implementation may map this mutex to one
              of the other mutex types.
              //这种类型下,多次锁住互斥量是未知的结果。试图解锁一个未加锁的互斥量,结果是未知的。试图去解锁一个
              //其他线程加锁的互斥量,结果是未知的。有的实现会将这种类型映射到其他类型

RETURN VALUE
       Upon successful completion, the pthread_mutexattr_gettype() function shall return zero and store the value of  the  type
       attribute  of  attr  into the object referenced by the type parameter. Otherwise, an error shall be returned to indicate
       the error.
        //成功返回0, pthread_mutexattr_gettype() 会保存类型值,失败返回错误码

       If successful, the pthread_mutexattr_settype() function shall return zero; otherwise, an error number shall be  returned
       to indicate the error.
        //成功返回0,失败返回错误码

ERRORS
       The pthread_mutexattr_settype() function shall fail if:
        //pthread_mutexattr_settype() 会在以下情况失败

       EINVAL The value type is invalid.
                    //类型值无效

       The pthread_mutexattr_gettype() and pthread_mutexattr_settype() functions may fail if:
        //pthread_mutexattr_gettype() and pthread_mutexattr_settype() 在以下情况失败

       EINVAL The value specified by attr is invalid.
                    //属性值是无效的

       These functions shall not return an error code of [EINTR].
        //不会返回EINTR

六、实例
    互斥量属性使用
    1、程序框架

    2、源代码

点击(此处)折叠或打开

  1. #include "apue.h"

  2. int main()
  3. {
  4.     char *shm = "myshm";
  5.     char *shm1 = "myshm1";
  6.     int shm_id, shm_id1;
  7.     char *buf;
  8.     pid_t pid;

  9.     pthread_mutex_t *mutex;
  10.     pthread_mutexattr_t mutexattr;


  11.     //打开共享内存
  12.     shm_id1 = shm_open(shm1, O_RDWR|O_CREAT, 0644);
  13.     //调整共享内存大小
  14.     ftruncate(shm_id1, 100);
  15.     //映射共享内存,MAP_SHARED属性表明,对共享内存的任何修改都会影响其他进程
  16.     mutex =(pthread_mutex_t *)mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id1, 0);

  17.     pthread_mutexattr_init(&mutexattr);
  18. #ifdef _POSIX_THREAD_PROCESS_SHARED
  19.     pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);
  20. #endif
  21.     pthread_mutex_init(mutex, &mutexattr);

  22.     //打开共享内存
  23.     shm_id = shm_open(shm, O_RDWR|O_CREAT, 0644);
  24.     //调整共享内存大小
  25.     ftruncate(shm_id, 100);
  26.     //映射共享内存,MAP_SHARED属性表明,对共享内存的任何修改都会影响其他进程
  27.     buf =(char *)mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id, 0);
  28.     
  29.     pid = fork();
  30.     if(pid==0)
  31.     {    
  32.         //休眠1s,让父进程先运行
  33.         sleep(1);
  34.         printf("I'm child proccess\n");
  35.         
  36.         pthread_mutex_lock(mutex);
  37.         //将共享内存内存修改为hello
  38.         memcpy(buf, "hello", 6);
  39.         printf("child buf is : %s\n", buf);
  40.         pthread_mutex_unlock(mutex);
  41.     }
  42.     else if(pid>0)
  43.     {
  44.         printf("I'm parent proccess\n");

  45.         pthread_mutex_lock(mutex);
  46.         //修改共享内存到内容,改为world
  47.         memcpy(buf, "world", 6);
  48.         sleep(3);
  49.         printf("parent buf is : %s\n", buf);
  50.         pthread_mutex_unlock(mutex);
  51.     }

  52.     pthread_mutexattr_destroy(&mutexattr);
  53.     pthread_mutex_destroy(mutex);
  54.     //解除映射
  55.     munmap(buf, 100);
  56.     //消除共享内存
  57.     shm_unlink(shm);
  58.     //解除映射
  59.     munmap(mutex, 100);
  60.     //消除共享内存
  61.     shm_unlink(shm1);
  62. }


   











 

阅读(15286) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~