linux线程与进程同步的不同,Linux多线程5-3_线程的同步属性

//头文件

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、程序框架

1ac3db598a5151d1921cacb9efa64d48.png

2、源代码

点击(此处)折叠或打开

#include "apue.h"

int main()

{

char *shm = "myshm";

char *shm1 = "myshm1";

int shm_id, shm_id1;

char *buf;

pid_t pid;

pthread_mutex_t *mutex;

pthread_mutexattr_t mutexattr;

//打开共享内存

shm_id1 = shm_open(shm1, O_RDWR|O_CREAT, 0644);

//调整共享内存大小

ftruncate(shm_id1, 100);

//映射共享内存,MAP_SHARED属性表明,对共享内存的任何修改都会影响其他进程

mutex =(pthread_mutex_t *)mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id1, 0);

pthread_mutexattr_init(&mutexattr);

#ifdef _POSIX_THREAD_PROCESS_SHARED

pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED);

#endif

pthread_mutex_init(mutex, &mutexattr);

//打开共享内存

shm_id = shm_open(shm, O_RDWR|O_CREAT, 0644);

//调整共享内存大小

ftruncate(shm_id, 100);

//映射共享内存,MAP_SHARED属性表明,对共享内存的任何修改都会影响其他进程

buf =(char *)mmap(NULL, 100, PROT_READ|PROT_WRITE, MAP_SHARED, shm_id, 0);

pid = fork();

if(pid==0)

{

//休眠1s,让父进程先运行

sleep(1);

printf("I'm child proccess\n");

pthread_mutex_lock(mutex);

//将共享内存内存修改为hello

memcpy(buf, "hello", 6);

printf("child buf is : %s\n", buf);

pthread_mutex_unlock(mutex);

}

else if(pid>0)

{

printf("I'm parent proccess\n");

pthread_mutex_lock(mutex);

//修改共享内存到内容,改为world

memcpy(buf, "world", 6);

sleep(3);

printf("parent buf is : %s\n", buf);

pthread_mutex_unlock(mutex);

}

pthread_mutexattr_destroy(&mutexattr);

pthread_mutex_destroy(mutex);

//解除映射

munmap(buf, 100);

//消除共享内存

shm_unlink(shm);

//解除映射

munmap(mutex, 100);

//消除共享内存

shm_unlink(shm1);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值