C语言多线程编程 死锁解析
1.假设有两个线程
A线程负责输出奇数。B线程负责输出偶数。
2.当A线程进入锁定状态是,主线程突然异常将A线程停止,这时将导致B线程也无法继续执行,处于死锁状态。如下代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t m; void *runodd(void *d)
{
int i=; for(i=;;i+=)
{
pthread_mutex_lock(&m);
printf("奇数:%d\n",i);
usleep();
pthread_mutex_unlock(&m);
}
}
void *runeven(void *d)
{
int i=;
for(i=;;i+=)
{
pthread_mutex_lock(&m);
printf("偶数:%d\n",i);
usleep();
pthread_mutex_unlock(&m);
}
}
main()
{
pthread_t todd,teven;
pthread_mutex_init(&m,);
pthread_create(&todd,,runodd,);
pthread_create(&teven,,runeven,);
sleep();
printf("外部强制停止todd线程\n");
pthread_cancel(todd);
pthread_join(todd,(void**));
pthread_join(teven,(void**));
pthread_mutex_destroy(&m);
}
解决方法:
运用2个函数(其实是2个宏)
pthread_cleanup_push
pthread_cleanup_pop 这个对函数作用类似于atexit函数
注意:这不是函数而是宏。必须成对使用。
void pthread_cleanup_push(
void (*routine)(void *),//回调函数
void *arg //回调函数的参数
);
触发调用routine的条件:
- 执行了exit()。
- 执行了pthread_cancel()
- pthread_cleanup_pop(1);//参数必须是1
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t m;
void handle(void *d)
{
printf("退出后的调用!\n");
pthread_mutex_unlock(&m);
}
void *runodd(void *d)
{
int i=; for(i=;;i+=)
{
pthread_cleanup_push(handle,);
pthread_mutex_lock(&m);
printf("奇数:%d\n",i);
usleep();
pthread_mutex_unlock(&m);
pthread_cleanup_pop();
}
}
void *runeven(void *d)
{
int i=;
for(i=;;i+=)
{
pthread_mutex_lock(&m);
printf("偶数:%d\n",i);
usleep();
pthread_mutex_unlock(&m);
}
}
C语言多线程编程 死锁解析的更多相关文章
- Linux C语言多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux多线程编程详细解析----条件变量 pthread_cond_t
Linux操作系统下的多线程编程详细解析----条件变量 1.初始化条件变量pthread_cond_init #include <pthread.h> int pthread_cond_ ...
- linux下C语言多线程编程实例
用一个实例.来学习linux下C语言多线程编程实例. 代码目的:通过创建两个线程来实现对一个数的递加.代码: //包含的头文件 #include <pthread.h> #include ...
- C语言多线程编程
HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUT ...
- Linux多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- C语言多线程编程二
一. 线程通信----事件: 1.一对一模式: #include <stdio.h> #include <stdlib.h> #include <Windows.h> ...
- Linux操作系统下的多线程编程详细解析----条件变量
条件变量通过允许线程阻塞和等待另一个线程发送信号的方法,弥补了互斥锁(Mutex)的不足. 1.初始化条件变量pthread_cond_init #include <pthread.h> ...
- iOS——多线程编程详细解析
基本定义: 程序:由代码生成的可执行应用.(例如QQ.app) 进程:一个正在运行的程序可以看做是一个进程. (例如:正在运行的QQ 就是一个进程),进程拥有独立运行所需要的全部资源. 线程: 程序中 ...
- C语言 之 多线程编程
一.基础知识 计算机的核心是CPU,承担了所有的计算任务. 操作系统是计算机的管理者,负责任务的调度.资源的分配和管理,统领整个计算机硬件. 应用程序则是具有某种功能的程序,程序是运行于操作系统之上的 ...
随机推荐
- python标准模块(二)
本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...
- Mysql学习笔记(八)由触发器回顾外键约束中的级联选项
近些天都没有写博客.在学习mysql的知识,通过学习和练习,也熟悉了mysql的函数.触发器.视图和存储过程.并且在实际的开发过程中也应用了一小部分.效果还是十分理想的. 今天晚上在学习触发器模仿in ...
- POJ2239 Selecting Courses(二分图最大匹配)
题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...
- 使用chrome查看网页上效果的实现方式
使用chrome查看网页上效果的实现方式 chrome是一个极为强大的工具,很多时候,我们不知道一个效果怎么实现的,我们完全可以找到响应的网页,然后找到其html文件,和js文件,查看源码,获得其实现 ...
- 添加JavaScrip
本章内容: 加载外部脚本:添加嵌入脚本:JavaScrip事件 1,脚本类型:外部文件(使用纯文本格式)加载的脚本:嵌入在页面中的脚本. 加载外部脚本的方法 <body><scrip ...
- ubuntu 安装php7.1
sudo apt-get update sudo apt-get install -y language-pack-en-base locale-gen en_US.UTF-8 sudo apt-ge ...
- JavaScript排序算法——归并排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C#和SQL实现的字符串相似度计算代码分享
http://www.jb51.net/article/55941.htm C#实现: 复制代码 代码如下: #region 计算字符串相似度 /// <summary> ...
- 我的linux桌面
经过几次尝试安装linux系统之后,终于把自己的系统安装成了linux系统. wangkongming@ThinkPad-T410 ~ $ lsb_release -a No LSB modules ...
- Linux下的特殊权限SetUID
1.SetUID的功能 只有可以执行的二进制程序才能设置SUID权限 命令执行者要对改程序拥有x执行权限 命令执行者在执行改程序的时候获得该程序文件属主的身份(在执行程序的过程中灵魂附体为文件的属性) ...