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,承担了所有的计算任务. 操作系统是计算机的管理者,负责任务的调度.资源的分配和管理,统领整个计算机硬件. 应用程序则是具有某种功能的程序,程序是运行于操作系统之上的 ...
随机推荐
- JS生成随机数的各种函数
第一种方法 /* *@desc:生成随机字符串 *@remark:toString方法可以接收一个基数作为参数的原理,这个基数从2到36封顶.如果不指定,默认基数是10进制 */ function g ...
- CSS的4种引入方式及优先级
第一:css的四种引入方式 1.行内样式 最直接最简单的一种,直接对HTML标签使用style="",例如: <p style="color:#F00; " ...
- Datatable的Select()
利用datatable的select方法筛选出符合条件的datarow进行操作 Select() Select(string filterExpression) Select(string filte ...
- 阻塞式socket例子学习
/************************************************************************* > File Name: Win_Serve ...
- EnableViewState
EnableViewState 系统默认的值为true,在传递状态值时就包括该控件: 为false,则传递状态值时则不包括它. 可以提高网络访问的速度. 某些控件是不需要接受用户的操作或只需要接受一次 ...
- Java数据结构的特点
- 关于linux发行版i386/i686/x86-64/的区别
http://blog.chinaunix.net/uid-20448327-id-172412.html
- Windows Platform Predefined Macros
https://msdn.microsoft.com/en-us/library/b0084kay.aspx
- Spot光照资料
http://forums.autodesk.com/t5/FBX-SDK/EmissiveFactor-AmbientFactor-DiffuseFactor/td-p/4230572http:// ...
- Elmah 日志记录组件
http://www.cnblogs.com/jys509/p/4571298.html 简介 ELMAH(Error Logging Modules and Handlers)错误日志记录模块和处理 ...