概述
锁类型
相关函数原型:
int pthread_mutexattr_settype (pthread_mutexattr_t *attr, int kind);
int pthread_mutexattr_gettype (const pthread_mutexattr_t *attr, int *kind);
kind 取值:
值 | 定义 | 别名 | 描述 |
0 | PTHREAD_MUTEX_TIMED_NP | PTHREAD_MUTEX_NORMAL | 普通锁,默认值,加锁后任何其他或本线程的加锁都会阻塞 |
1 | PTHREAD_MUTEX_RECURSIVE_NP | PTHREAD_MUTEX_RECURSIVE | 嵌套锁,允许同线程内对同一个锁加锁多次,记录加锁次数 |
2 | PTHREAD_MUTEX_ERRORCHECK_NP | PTHREAD_MUTEX_ERRORCHECK | 检错锁,禁止同线程内对同一个锁加锁多次 |
3 | PTHREAD_MUTEX_ADAPTIVE_NP | 适应锁,效率更高,等同于多次trylock() + PTHREAD_MUTEX_TIMED_NP |
#include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; int main(int argc, char* argv[]) { //初始化锁类型 pthread_mutexattr_t mutex_attr; pthread_mutexattr_init(&mutex_attr); pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_TIMED_NP); //初始化锁 pthread_mutex_init(&mutex, &mutex_attr); //..业务处理... return 0; }
进程间锁
相关函数原型:
int pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, int pshared);
int pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr, int *pshared);
pshared 取值:
值 | 定义 | 描述 |
0 | PTHREAD_PROCESS_PRIVATE | 进程内互斥锁,仅可当前进程内共享 |
1 | PTHREAD_PROCESS_SHARED | 进程间互斥锁,多个进程间共享 |
第一个程序代码
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <sys/mman.h> #include <fcntl.h> #include <string.h> #include <sys/stat.h> int main(int argc, const char* argv[]) { char mmap_file_path[512] = { 0 }; getcwd(mmap_file_path, sizeof(mmap_file_path)); strcat(mmap_file_path, "/pthread_shared"); pthread_mutex_t* mutex_ptr = NULL; int fd = open(mmap_file_path, O_RDWR | O_CREAT, 0744); if (0 < fd) { //重置文件大小 ftruncate(fd, sizeof(pthread_mutex_t)); mutex_ptr = (pthread_mutex_t*)mmap(0, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); } else { printf("open errn"); } if (NULL != mutex_ptr) { //初始化锁对象 pthread_mutexattr_t attr; pthread_mutexattr_init(&attr); pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); pthread_mutex_init(mutex_ptr, &attr); int lock_ret = pthread_mutex_lock(mutex_ptr); if (0 == lock_ret) { printf("lock successedn"); usleep(10 * 1000 * 1000); pthread_mutex_unlock(mutex_ptr); } else { printf("lock failed: %dn", lock_ret); } } return 0; }
第二个程序代码,特殊处在获取锁对象后不需要初始化,因为在第一个进程内已经初始化了
#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <sys/mman.h> #include <fcntl.h> #include <string.h> int main(int argc, const char* argv[]) { char mmap_file_path[512] = { 0 }; getcwd(mmap_file_path, sizeof(mmap_file_path)); strcat(mmap_file_path, "/pthread_shared"); pthread_mutex_t* mutex_ptr = NULL; int fd = open(mmap_file_path, O_RDWR | O_CREAT, 0744); if (0 < fd) { mutex_ptr = (pthread_mutex_t*)mmap(0, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); close(fd); } else { printf("open errn"); } if (NULL != mutex_ptr) { int lock_ret = pthread_mutex_lock(mutex_ptr); if (0 == lock_ret) { printf("lock successedn"); usleep(10 * 1000 * 1000); pthread_mutex_unlock(mutex_ptr); } else { printf("lock failed: %dn", lock_ret); } } return 0; }
锁优先级
设置持有互斥量的线程的优先级上限。相关函数的原型:
int pthread_mutexattr_setprioceiling (pthread_mutexattr_t *attr, int prioceiling);
int pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *attr, int *prioceiling);
Linux 上 prioceiling 取值范围 [1, 99],值越大级别越高。
锁协议
相关函数的原型:
int pthread_mutexattr_setprotocol (pthread_mutexattr_t *attr, int protocol);
int pthread_mutexattr_getprotocol (const pthread_mutexattr_t *attr, int *protocol);
protocol取值
值 | 定义 | 描述 |
0 | PTHREAD_PRIO_NONE | 不改变线程的优先级属性 |
1 | PTHREAD_PRIO_INHERIT | 递归的,持有锁的线程,将提升优先级至被该锁阻塞的其他线程里的最高优先级 |
2 | PTHREAD_PRIO_PROTECT | 以线程本身优先级和持有锁的优先级的最高者运行,无论是否有其他线程在等待该锁 |
锁健壮性
相关函数的原型:
int pthread_mutexattr_setrobust_np (pthread_mutexattr_t *attr, int robustness);
int pthread_mutexattr_getrobust_np (const pthread_mutexattr_t *attr, int *robustness);
robustness 取值
值 | 定义 | 描述 |
0 | PTHREAD_MUTEX_STALLED_NP | 如果互斥锁的属主死亡,则以后对 pthread_mutex_lock() 的所有调用将以不确定的方式被阻塞 |
1 | PTHREAD_MUTEX_ROBUST_NP | 互斥锁的属主死亡时,将会解除锁定该互斥锁。互斥锁的下一个属主将获取该互斥锁,并返回错误EOWNWERDEAD |
转载于:https://www.cnblogs.com/tianrks/p/10785577.html
最后
以上就是迷路钢笔为你收集整理的pthread mutexattr锁类型进程间锁锁优先级锁协议锁健壮性的全部内容,希望文章能够帮你解决pthread mutexattr锁类型进程间锁锁优先级锁协议锁健壮性所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复