C++死锁解决心得
一、 概述
C++多线程开发中,容易出现死锁导致程序挂起的现象。
关于死锁的信息,见百度百科http://baike.baidu.com/view/121723.htm。
解决步骤分为三步:
1、检测死锁线程。
2、打印线程信息。
3、修改死锁程序。
二、 程序示例
VS2005创建支持MFC的win32控制台程序。
代码见示例代码DeadLockTest.cpp。
- // DeadLockTest.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "DeadLockTest.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // The one and only application object
- CWinApp theApp;
- using namespace std;
- CRITICAL_SECTION cs1;
- CRITICAL_SECTION cs2;
- CRITICAL_SECTION csprint;
- //初始化关键代码段
- void InitMyCriticalSection();
- //删除关键代码段
- void DeleteMyCriticalSection();
- //打印信息
- void PrintString(const CString& strInfo);
- DWORD WINAPI Thread1(LPVOID lpParameter);
- DWORD WINAPI Thread2(LPVOID lpParameter);
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int nRetCode = 0;
- // initialize MFC and print and error on failure
- if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
- {
- // TODO: change error code to suit your needs
- _tprintf(_T("Fatal Error: MFC initialization failed\n"));
- nRetCode = 1;
- return nRetCode;
- }
- //初始化关键代码段
- InitMyCriticalSection();
- //创建线程
- HANDLE hThread1 = CreateThread(NULL, 0, Thread1, NULL, 0, NULL);
- HANDLE hThread2 = CreateThread(NULL, 0, Thread2, NULL, 0, NULL);
- //等待线程结束
- WaitForSingleObject(hThread1, INFINITE);
- WaitForSingleObject(hThread2, INFINITE);
- //关闭线程句柄
- CloseHandle(hThread1);
- CloseHandle(hThread2);
- //释放关键代码段
- DeleteMyCriticalSection();
- return nRetCode;
- }
- void InitMyCriticalSection()
- {
- InitializeCriticalSection(&cs1);
- InitializeCriticalSection(&cs2);
- InitializeCriticalSection(&csprint);
- }
- void DeleteMyCriticalSection()
- {
- DeleteCriticalSection(&cs1);
- DeleteCriticalSection(&cs2);
- DeleteCriticalSection(&csprint);
- }
- DWORD WINAPI Thread1(LPVOID lpParameter)
- {
- for (int i = 0; i < 5; i++)
- {
- EnterCriticalSection(&cs1);
- Sleep(500);
- EnterCriticalSection(&cs2);
- PrintString(_T("Thread1"));
- LeaveCriticalSection(&cs2);
- LeaveCriticalSection(&cs1);
- }
- return 1;
- }
- DWORD WINAPI Thread2(LPVOID lpParameter)
- {
- for (int i = 0; i < 5; i++)
- {
- EnterCriticalSection(&cs2);
- Sleep(500);
- EnterCriticalSection(&cs1);
- PrintString(_T("Thread2"));
- LeaveCriticalSection(&cs1);
- LeaveCriticalSection(&cs2);
- }
- return 1;
- }
- void PrintString(const CString& strInfo)
- {
- EnterCriticalSection(&csprint);
- wcout<<(const TCHAR*)strInfo<<endl;
- LeaveCriticalSection(&csprint);
- }
运行DeadLockTest.exe,程序挂起。
三、 死锁检测
检测工具见《Windows核心编程》,第9章9.8.6节LockCop检测工具。
工具源码地址:http://www1.wintellect.com/Resources/Details/86。
LockCop可使用vs2010编译成功。
备注:该工具使用了Windows Vista/ 7提供的WCT API,故需要在Windows Vista/ 7系统运行LockCop检测工具。
检测,挂起的DeadLockTest.exe,得到线程信息。
检测到程序挂起由死锁引起。
线程4014:等待线程772、线程4012完成。
线程772:拥有关键代码段A,等待关键代码段B(被线程4012拥有)。
线程4012:拥有关键代码段B,等待关键代码段A(被线程772拥有)。
线程772与4012互相等待,程序发生死锁现象。
四、 打印信息
为了便于查找问题,我们加上线程打印信息。
打印线程名称、线程ID以及关键代码段进入信息。
- DWORD WINAPI Thread1(LPVOID lpParameter)
- {
- CString strThreadID = _T("");
- strThreadID.Format(_T("%d"), GetCurrentThreadId());
- CString strPrintInfo = _T("");
- for (int i = 0; i < 5; i++)
- {
- EnterCriticalSection(&cs1);
- strPrintInfo = _T("");
- strPrintInfo += _T("Thread1 ");
- strPrintInfo += strThreadID;
- strPrintInfo += _T(" EnterCriticalSection(&cs1)");
- PrintString(strPrintInfo);
- Sleep(500);
- EnterCriticalSection(&cs2);
- strPrintInfo = _T("");
- strPrintInfo += _T("Thread1 ");
- strPrintInfo += strThreadID;
- strPrintInfo += _T(" EnterCriticalSection(&cs2)");
- PrintString(strPrintInfo);
- LeaveCriticalSection(&cs2);
- LeaveCriticalSection(&cs1);
- }
- return 1;
- }
- DWORD WINAPI Thread2(LPVOID lpParameter)
- {
- CString strThreadID = _T("");
- strThreadID.Format(_T("%d"), GetCurrentThreadId());
- CString strPrintInfo = _T("");
- for (int i = 0; i < 5; i++)
- {
- EnterCriticalSection(&cs2);
- strPrintInfo = _T("");
- strPrintInfo += _T("Thread2 ");
- strPrintInfo += strThreadID;
- strPrintInfo += _T(" EnterCriticalSection(&cs2)");
- PrintString(strPrintInfo);
- Sleep(500);
- EnterCriticalSection(&cs1);
- strPrintInfo = _T("");
- strPrintInfo += _T("Thread2 ");
- strPrintInfo += strThreadID;
- strPrintInfo += _T(" EnterCriticalSection(&cs1)");
- PrintString(strPrintInfo);
- LeaveCriticalSection(&cs1);
- LeaveCriticalSection(&cs2);
- }
- return 1;
- }
运行结果如下。
五、 死锁修改
线程互斥进行修改,Thread1与Thread2对关键代码段的进入与退出顺序改为相同。程序运行正常。
修改后线程代码。
- DWORD WINAPI Thread1(LPVOID lpParameter)
- {
- for (int i = 0; i < 5; i++)
- {
- EnterCriticalSection(&cs1);
- Sleep(500);
- EnterCriticalSection(&cs2);
- PrintString(_T("Thread1"));
- LeaveCriticalSection(&cs2);
- LeaveCriticalSection(&cs1);
- }
- return 1;
- }
- DWORD WINAPI Thread2(LPVOID lpParameter)
- {
- for (int i = 0; i < 5; i++)
- {
- EnterCriticalSection(&cs1);
- Sleep(500);
- EnterCriticalSection(&cs2);
- PrintString(_T("Thread2"));
- LeaveCriticalSection(&cs2);
- LeaveCriticalSection(&cs1);
- }
- return 1;
- }
C++死锁解决心得的更多相关文章
- SqlServer定时备份数据库和定时杀死数据库死锁解决
上周五组长对我说了一句要杀死数据库的死锁进程,有时候同一时刻不停写入数据库会造成这种情况的发生,因为自己对数据库不是很熟悉,突然组长说了我也就决定一定要倒腾一下,不然自己怎么提高呢?现在不研究,说不定 ...
- Oracle 表死锁 解决
问题:更新的Update语句一直在更新 卡在执行update语句的地方. 清除的方法: Oracle表死锁解除 我是在plsql中处理 1.先查询 select * from v$locked ...
- sqlserver2008 死锁解决方法及性能优化方法
sqlserver2008 死锁解决方法及性能优化方法 原文: http://blog.csdn.net/kuui_chiu/article/details/48621939 十步优化SQL Serv ...
- jvm死锁解决
那我们怎么确定一定是死锁呢?有两种方法. 1>使用JDK给我们的的工具JConsole,可以通过打开cmd然后输入jconsole打开. 1)连接到需要查看的进程.
- mysql死锁+解决
自己作死,navicat不恰当的操作导致了表死锁,操作如下: 给表新加字段:name 没有选择允许为空,但是有没有设置初始值,所以运行的结果就是数据库表里有了name不允许为空但是确实为空的记录: 然 ...
- SQL Server 表,记录 死锁解决办法
我自己的数据库表记录死锁后的 根据以下资料的 解决方案: 1. 先根据以下语句 查询 哪些表被 死锁,及 死锁的 spid SELECT request_session_id spid,OBJECT ...
- PL/SQL 出现死锁解决办法
转自:https://blog.csdn.net/u013015629/article/details/48005763 在PL/SQL中操作数据表时,长时间没反应,并且编辑某个表中数据时,出现“re ...
- ADO访问Access数据库错误解决心得随笔
最近在用ADO访问Access数据库的时候出现了一个奇怪的错误,觉得有必要记录下来,和大家分享一下. 环境 win7 x86系统: VS2012编译器: Office2010: Access2000~ ...
- DB2死锁解决办法
db2 命令行,1.用管理员用户登录:db2 connect to 你的数据库名 user 用户名 using 密码 2.db2 "get snapshot for locks on 数据库 ...
随机推荐
- 一元云购qq互联回调地址错误解决办法
经过追踪,点击登录后调用 system/modules/api/下面的qqlogin.action.class.php 里面又调用了qq 互联php接口样例里的QC.php的QC类的方法qq_logi ...
- sshd被攻击的自动防御方法v2
1.增加了“频繁攻击的封锁时间”,即设置为上次攻击时间的2倍 2.加入了数据库支持,将攻击者相关信息记录入库,如攻击者ip.攻击次数.封锁时间 3.简化了代码 具体实现步骤如下: 1.创建 ...
- [Django] Windows 下安装 配置Pinax 工程
Pinax 是一个基于Django开发的脚手架,有一些现成的模板和功能模块可以使用,方便快速有效的开发一个Django项目.下面举个例子如何安装一个pinax项目到集成开发环境Aptana里面. 先从 ...
- HttpContext.Current.Cache 过期时间
原文:HttpContext.Current.Cache 过期时间 为了更快的读取数据,我们一般会把常用到的数据加载到Cache中 在.NET中,Cache的存在可以依赖多中方式,主要用到HttpCo ...
- 从ASP.NET传递参数给水晶报表
原文 http://www.cnblogs.com/insus/p/3281114.html 上次Insus.NET有简单写了一篇文章<Visual Studio 2012使用水晶报表Cryst ...
- N个数依次入栈,出栈顺序有多少种
题目:N个数依次入栈,出栈顺序有多少种? 首先介绍一下卡特兰数:卡特兰数前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 2 ...
- iOS中如何呼出另一个应用
我们经常会遇到在一个应用里面呼出另一个应用的需求,比如在文档里面点击地址,调用safari来打开网页:比如在文件浏览器里面点击某种文件,自动激活一个应用来打开文件. iOS里面对于这样的需求使用URL ...
- Spring、Bean 的作用域
Singleton作用域(默认) 当一个bean的作用域为singleton,那么Spring Ioc容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则 ...
- 网易云课堂_程序设计入门-C语言_第五周:函数_1分解质因数
1 分解质因数(5分) 题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. ...
- 学习wxpython的网站
http://xoomer.virgilio.it/infinity77/Phoenix/main.html https://wxpython.org/Phoenix/docs/html/main.h ...