linux线程间同步(1)互斥锁与条件变量

线程的最大特点是资源的共享性,但资源共享中的同步问题是多线程编程的难点。linux下提供了多种方式来处理线程同步,最常用的是互斥锁、条件变量和信号量以及读写锁。

互斥锁(mutex)

互斥锁,是一种信号量,常用来防止两个进程或线程在同一时刻访问相同的共享资源。可以保证以下三点:

  • 原子性:把一个互斥量锁定为一个原子操作,这意味着操作系统(或pthread函数库)保证了如果一个线程锁定了一个互斥量,没有其他线程在同一时间可以成功锁定这个互斥量。
  • 唯一性:如果一个线程锁定了一个互斥量,在它解除锁定之前,没有其他线程可以锁定这个互斥量。
  • 非繁忙等待:如果一个线程已经锁定了一个互斥量,第二个线程又试图去锁定这个互斥量,则第二个线程将被挂起(不占用任何cpu资源),直到第一个线程解除对这个互斥量的锁定为止,第二个线程则被唤醒并继续执行,同时锁定这个互斥量。

从以上三点,我们看出可以用互斥量来保证对变量(关键的代码段)的排他性访问。

互斥锁常用函数

#include <pthread.h>//初始化互斥锁
int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr);
//互斥锁静态赋值
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//阻塞加锁
int pthread_mutex_lock(pthread_mutex_t *mutex);
//非阻塞加锁,成功则返回0,否则返回EBUSY
int pthread_mutex_trylock(pthread_mutex_t *mutex);
//解锁
int pthread_mutex_unlock(pthread_mutex_t *mutex);//销毁互斥锁
int pthread_mutex_destroy(pthread_mutex_t *mutex);

如果要正确的使用pthread_mutex_lock与pthread_mutex_unlock,请参考pthread_cleanup_push和pthread_cleanup_pop宏,它能够在线程被cancel的时候正确的释放mutex!

互斥锁属性

#include <pthread.h>int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
int pthread_mutexattr_init(pthread_mutexattr_t *attr);//锁的范围:PTHREAD_PROCESS_PRIVATE(进程内),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);//锁的类型
int pthread_mutexattr_gettype(const pthread_mutexattr_t *restrict attr,int *restrict type);
int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *restrict attr, int *restrict protocol);
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *attr,int protocol);int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *restrict attr, int *restrict prioceiling);
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *attr,int prioceiling);   

说明:pthread库不是Linux系统默认的库,连接时需要使用静态库libpthread.a,所以在使用pthread_create()创建线程,以及调用pthread_atfork()函数建立fork处理程序时,需要链接该库。在编译中要加 -lpthread参数。

条件变量(cond)

利用线程间共享的全局变量进行同步的一种机制。条件变量上的基本操作有:触发条件(当条件变为 true 时);等待条件,挂起线程直到其他线程触发条件。

int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);int pthread_cond_destroy(pthread_cond_t *cond);int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有线程的阻塞

使用说明:

  • 动态初始化调用pthread_cond_init()或者pthread_cond_t cond=PTHREAD_COND_INITIALIER静态初始化;属性置为NULL

  • pthread_cond_wait与pthread_cond_timedwait,在使用前应用程序必须执行了加锁互斥量,两函数在调用时自动解锁互斥量,等待条件互斥量触发。这时线程挂起,不占用CPU,前者直到条件变量被触发,后者等待条件变量被触发或者超时(返回ETIMEOUT)。函数返回前,自动重新对互斥量自动加锁。

  • 互斥量的解锁和在条件变量上挂起都是自动进行的。因此,在条件变量被触发前,如果所有的线程都要对互斥量加锁,这种机制可保证在线程加锁互斥量和进入等待条件变量期间,条件变量不被触发。条件变量要和互斥量相联结,以避免出现条件竞争— —个线程预备等待一个条件变量,当它在真正进入等待之前,另一个线程恰好触发了该条件(条件满足信号有可能在测试条件和调用pthread_cond_wait函数(block)之间被发出,从而造成无限制的等待)。

  • pthread_cond_destroy 销毁一个条件变量,释放它拥有的资源。进入 pthread_cond_destroy 之前,必须没有在该条件变量上等待的线程,否则返回EBUSY

  • 条件变量函数不是异步信号安全的,不应当在信号处理程序中进行调用。特别要注意,如果在信号处理程序中调用 pthread_cond_signal 或 pthread_cond_boardcast 函数,可能导致调用线程死锁。pthread_cond_signal与pthread_cond_broadcast无需考虑调用线程是否是mutex的拥有者,也就是说,可以在lock与unlock以外的区域调用。如果我们对调用行为不关心,那么请在lock区域之外调用吧。

代码实例

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;struct node
{int n_number;struct node *n_next;
} *head = NULL;static void cleanup_handler(void *arg)
{printf("Cleanup handler of second thread./n");free(arg);(void)pthread_mutex_unlock(&mtx);
}static void *thread_func(void *arg)
{struct node *p = NULL;pthread_cleanup_push(cleanup_handler, p);while (1){//这个mutex主要是用来保证pthread_cond_wait的并发性pthread_mutex_lock(&mtx);while (head == NULL){//这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何//这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线//程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况。//这个时候,应该让线程继续进入pthread_cond_wait// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,//然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立//而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源//用这个流程是比较清楚的pthread_cond_wait(&cond, &mtx);p = head;head = head->n_next;printf("Got %d from front of queue/n", p->n_number);free(p);}pthread_mutex_unlock(&mtx); //临界区数据操作完毕,释放互斥锁}pthread_cleanup_pop(0);return 0;
}int main(int argc, char *argv[])
{pthread_t tid;int i;struct node *p;//子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,而//不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大pthread_create(&tid, NULL, thread_func, NULL);sleep(1);for (i = 0; i < 10; i++){p = (struct node*)malloc(sizeof(struct node));p->n_number = i;pthread_mutex_lock(&mtx); //需要操作head这个临界资源,先加锁,p->n_next = head;head = p;pthread_cond_signal(&cond);pthread_mutex_unlock(&mtx); //解锁sleep(1);}printf("thread 1 wanna end the line.So cancel thread 2./n");//关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,退出//线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了。pthread_cancel(tid);pthread_join(tid, NULL);return 0;
}

线程取消点

一般情况下,线程在其主体函数退出的时候会自动终止,但同时也可以因为接收到另一个线程发来的终止(取消)请求而强制终止。

相关概念

线程取消的方法是向目标线程发送Cancel信号,但如何处理Cancel信号则由目标线程自己决定,或者忽略、或者立即终止、或者继续运行至Cancelation-point(取消点),由不同的Cancelation状态决定。

线程接收到CANCEL信号的缺省处理(即pthread_create()创建线程的缺省状态)是继续运行至取消点,也就是说设置一个CANCELED状态,线程继续运行,只有运行至Cancelation-point的时候才会退出。

根据POSIX标准,pthread_join()、pthread_testcancel()、pthread_cond_wait()、 pthread_cond_timedwait()、sem_wait()、sigwait()等函数以及read()、write()等会引起阻塞的系统调用都是Cancelation-point,而其他pthread函数都不会引起Cancelation动作。但是pthread_cancel的手册页声称,由于LinuxThread库与C库结合得不好,因而目前C库函数都不是Cancelation-point;但CANCEL信号会使线程从阻塞的系统调用中退出,并置EINTR错误码,因此可以在需要作为Cancelation-point的系统调用前后调用 pthread_testcancel(),从而达到POSIX标准所要求的目标.

相关API

  1. 取消线程运行
    int pthread_cancel(pthread_t thread);发送终止信号给thread线程,成功则返回0,否则返回非0. 成功发送并不意味着thread会终止.

  2. 设置取消点
    如果线程处于无限循环中,且循环体内没有执行至取消点的必然路径,则线程无法由外部其他线程的取消请求而终止。因此在这样的循环体的必经路径上应该加入pthread_testcancel()调用。
    pthread_testcancel:

    • 设置取消点
    • 检查本线程是否处于Canceld状态,如果是,则进行取消动作,否则直接返回。
  3. 设置线程取消状态与类型

    int pthread_setcancelstate(int state, int *oldstate);
    int pthread_setcanceltype(int type, int *oldtype);

    pthread_setcancelstate设置线程对Cancel的反应,PTHREAD_CANCEL_ENABLE(default)与PTHREAD_CANCEL_DISABLE分别表示接受信号后设为CANCEL转态或者忽略CANCEL信号继续运行

    pthread_setcanceltype设置本线程取消动作的执行时机,PTHREAD_CANCEL_DEFFERED(default)和PTHREAD_CANCEL_ASYCHRONOUS,仅当Cancel状态为Enable时有效,分别表示收到信号后继续运行至下一个取消点再退出和立即执行取消动作(退出).

代码实例

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>#define THREAD_MAX 4pthread_mutex_t mutex;
pthread_t thread[THREAD_MAX];static int tries;
static int started;void print_it(int *arg)
{pthread_t tid;tid = pthread_self();printf("Thread %lx was canceled on its %d try.\n",tid,*arg);
}void *Search_Num(int arg)
{pthread_t tid;int num;int k=0,h=0,j;int ntries;tid = pthread_self();srand(arg);num = rand()&0xFFFFFF;printf("thread num %lx\n",tid);ntries = 0;//默认设置//pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);//pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);pthread_cleanup_push((void *)print_it,(void *)&ntries);while(1) {num = (num+1)&0xffffff;ntries++;if(arg == num){//只允许一个线程操作此处while(pthread_mutex_trylock(&mutex) == EBUSY) { //一个线程操作后其余线程进入次循环挂起,等待pthread_cancel函数发送cancel信号终止线程k++;if(k == 100) {printf("----------2busy2-----------\n");}pthread_testcancel();}tries = ntries;//pthread_mutex_unlock(&mutex);   //如果加上这句话,将会有好几个线程找到主函数中设定的值pidprintf("Thread %lx found the number!\n",tid);for(j = 0;j<THREAD_MAX;j++) {if(thread[j] != tid) {pthread_cancel(thread[j]);}}break;}if (ntries % 100 == 0) {h++;/*线程阻塞,其他线程争夺资源,或者是等待pthread_cancel函数发送cancel信号终止线程*/pthread_testcancel();/*这是为了弄明白pthread_testcancel函数的作用而设置的代码段*/if(h == 10000){h = 0;printf("----------thread num %lx-------------\n",tid);}}}pthread_cleanup_pop(0);return (void *)0;
}int main()
{int i,pid;pid = getpid(); //设置要查找的数pthread_mutex_init(&mutex,NULL);printf("Search the num of %d\n",pid);for (started = 0; started < THREAD_MAX; started++) {pthread_create(&thread[started],NULL,(void *)Search_Num,(void *)pid);}for (i = 0; i < THREAD_MAX; i++) {pthread_join(thread[i],NULL);}printf("It took %d tries ot find the number!\n",tries);return 0;
}

参考:

  • 多线程编程之线程取消

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/508672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下压缩包生成与解压命令以及进度

不同后缀压缩包的打包与加压命令 .tar 解包&#xff1a;tar xvf FileName.tar打包&#xff1a;tar cvf FileName.tar DirName .gz 解压1&#xff1a;gunzip FileName.gz解压2&#xff1a;gzip -d FileName.gz压缩&#xff1a;gzip FileName .tar.gz 和 .tgz 解压&#xff1a;…

经典面试题

谷歌面试题&#xff1a;1024! 末尾有多少个0&#xff1f; 末尾0的个数取决于乘法中因子2和5的个数。显然乘法中因子2的个数大于5的个数&#xff0c;所以我们只需统计因子5的个数。 是5的倍数的数有&#xff1a; 1024 / 5 204个;对于25,50这些数据统计一次,但实际的是包含两个…

共享内存:mmap函数实现

内存映射的应用: 以页面为单位,将一个普通文件映射到内存中,通常在需要对文件进行频繁读写时使用,这样用内存读写取代I/O读写,以获得较高的性能;将特殊文件进行匿名内存映射&#xff0c;可以为关联进程提供共享内存空间;为无关联的进程提供共享内存空间&#xff0c;一般也是将…

MSYS2开发环境搭建

MSYS2开发环境搭建 软件安装 下载msys2-x86_64软件包 https://www.msys2.org/&#xff0c;双击安装到某根目录下&#xff0c;比如D:\msys64。 pacman是MSYS2自带的软件管理工具&#xff1a; 可通过修改msys64\etc\pacman.d下的三个文件修改软件源&#xff0c;可供选择的源有…

设置python路径

在python开发应用&#xff0c;我们多数是通过pip、easy_install等工具将需要的python安装到自己机子上就可以应用了&#xff0c;但是我们完成开发给用户使用时&#xff0c;程序运行环境就是一个问题。当然&#xff0c;你可以要求客户按照你的方法安装依赖的库&#xff0c;这种方…

linux动态库查找路径以及依赖关系梳理

编译时与运行时库的路径 linux下&#xff0c;编译时与运行时库的搜索路径是不同的 运行时动态库的路径搜索顺序 LD_PRELOAD环境变量&#xff0c;一般用于hack 编译目标代码时指定的动态库搜索路径(指的是用 -wl,rpath 或-R选项而不是-L)&#xff0c;readelf -d命令可以查看编…

eclipse--android开发环境搭建教程

引言 在windows安装Android的开发环境不简单也说不上算复杂&#xff0c;但由于国内无法正常访问google给android开发环境搭建带来不小的麻烦。现将本人搭建过程记录如下&#xff0c;希望会对投身android开发的小伙伴有所帮助。 android开发环境部署过程 安装JDK环境 下载安装…

pip工具使用总结以及常用库PIL、freetype的安装

pip工具安装使用 pip为python库软件管理工具pip docs 安装 wget https://bootstrap.pypa.io/ez_setup.py -O - | python 安装setuptools https://pypi.python.org/pypi/setuptoolswget https://bootstrap.pypa.io/get-pip.py -O - | python 安装pip工具 ttps://pypi.python.…

【技巧】Chrome应用技巧

把Chrome浏览器变成文本编辑器 在浏览器地址栏中输入一行代码&#xff1a;data:text/html, <html contenteditable>&#xff0c;回车即可把浏览器变临时编辑器。【CtrlShiftJ】调出JavaScript控制台&#xff0c;在控制台输入&#xff1a; document.body.contentEditable…

eclipse--python开发环境搭建

pydev插件介绍 PyDev is a Python IDE for Eclipse pydev官方网站&#xff1a;http://www.pydev.org/ 在Eclipse中安装pydev插件 启动Eclipse, 点击Help->Install New Software… 在弹出的对话框中&#xff0c;点Add 按钮。 Name中填:Pydev, Location中填http://pydev.or…

Win7虚拟无线AP以及Android手机抓包

设备要求 Windows7操作系统装有无线网卡的笔记本或台式机无线网卡必须支持“承载网络” 查看无线网卡是否支持“承载” 方法一: 开始菜单→所有程序→附件→命令提示符→右键“以管理员权限运行”; 键入命令“netsh wlan show drivers”,查看“支持承载网络”这一项,如果是…

CMD命令之BAT脚本路径信息

CD命令解疑 cd是chdir的缩写&#xff0c;命令详解参见cd /? 可以看到/d参数的解释如下&#xff1a; 使用 /D命令行开关&#xff0c;除了改变驱动器的当前目录之外&#xff0c;还可改变当前驱动器。 通常我们在xp系统中打开cmd窗口时&#xff0c;会显示 C:\Documents and Se…

Ubuntu开发环境搭建

在虚拟中试玩Ubuntu1604版本&#xff0c;有关安装后一些配置记录如下&#xff0c;以备后用。 简单设置 root密码设置 虚拟机安装完成后&#xff0c;默认不弃用root用户&#xff0c;需要给root设置密码后使用 sudo passwd root终端加入右键中 将终端加入右键后&#xff0c;在某…

Python GUI Programming (Tkinter)

Tkinter编程实例 #!/usr/bin/python #coding:utf-8from Tkinter import * import sysreload(sys) sys.setdefaultencoding(utf-8)class GUI_WINDOWS:def __init__(self, root):self.root Frame(root)self.driver Noneself.friendEdit Noneself.bStopQuery Falseself.loadF…

【ubuntu 22.04】安装vscode并配置正常访问应用商店

注意&#xff1a;要去vscode官网下载deb安装包&#xff0c;在软件商店下载的版本不支持输入中文 在ubuntu下用火狐浏览器无法访问vscode官网&#xff0c;此时可以手动进行DNS解析&#xff0c;打开DNS在线查询工具&#xff0c;解析以下主机地址&#xff08;复制最后一个IP地址&a…

Appium安装使用总结

开发环境搭建 搭建java JDK与Android SDK环境下载安装nodejs下载安装Appium 问题答疑 在测试中adb devices查询无缘无故多出陌生的虚拟设备 同事的经验是重启电脑&#xff0c;莫名其妙的设备消失&#xff1b;自我总结就是重启adb服务&#xff0c;adb kill-server&#xff0…

Python使用笔记总结目录

从2012年接触python&#xff0c;陆陆续续使用python将近3年。从最开始的使用python进行xml解析&#xff0c;到使用python做爬虫开发&#xff0c;再到最近半年的使用深度学习进行图像识别&#xff0c;python简洁易用&#xff0c;丰富的资源库&#xff0c;活跃的社区&#xff0c;…

原始套接字编程(1)

Linux下原始套接字的原理 创建原始套接字&#xff1a; socket(AF_NET, SOCK_RAW, protocol);1. 参数protocol用来致命所接收的协议包&#xff0c;如果是像IPPROTO_TCP(6)这种非0、非255的协议&#xff0c;能接收ip头为protocol域的数据包&#xff0c;包括IP头&#xff0c;协议…

VS 使用技能总结

常用快捷键 复制/剪切/删除整行代码 1&#xff09;如果你想复制一整行代码&#xff0c;只需将光标移至该行&#xff0c;再使用组合键“CtrlC”来完成复制操作&#xff0c;而无需选择整行。 2&#xff09;如果你想剪切一整行代码&#xff0c;只需将光标移至该行&#xff0c;再使…

vim使用总结

vim使用命令 【自动补全操作】 Ctrl n / Ctrl p 自动补全字符串Ctrl x Ctrl f 自动补全文件名 【多文件操作】 打开多个文件&#xff1a; vim file1 file2 … filen 在同一个窗口中打开所有想要打开的文件vim -o file1 file2 … filen 打开一个编辑会话&#xff0c;水平分…