100分求教一个最简单的CRITICAL_SECTION问题
2007-01-15 12:52
260 查看
今天发现自己工程里同步使用CRITICAL_SECTION变量的地方根本就没有锁住,
InitializeCriticalSection( )变量后,不管多少次EnterCriticalSection( )都能进去
调试很久不知道是哪里的问题,写了个最简单的测试程序(win32控制台程序,使用mfc库),
还是有问题.测试程序如下
#include "stdafx.h"
#include "testLock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinApp theApp;
using namespace std;
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
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}
CRITICAL_SECTION Sec;
InitializeCriticalSection( &Sec );
EnterCriticalSection( &Sec ); ///能调用返回,返回后
///Sec.LockCount=0,RecursionCount=1
EnterCriticalSection( &Sec ); ///还是能返回,没招,返回后
///Sec.LockCount=1,RecursionCount=2
return nRetCode;
}
有经验的大侠指点下,不胜感激~~
---------------------------------------------------------------
CRITICAL_SECTION CriticalSection;
// Initialize the critical section.
InitializeCriticalSection(&CriticalSection);
// Request ownership of the critical section.
__try
{
EnterCriticalSection(&CriticalSection);
// Access the shared resource.
}
__finally
{
// Release ownership of the critical section.
LeaveCriticalSection(&CriticalSection);
// Release resources used by the critical section object.
DeleteCriticalSection(&CriticalSection)
}
---------------------------------------------------------------
都不理解CRITICAL_SECTION 用来什么的.
所谓CRITICAL_SECTION 是用来处理"一份被共享的资源", 如一段内存, 一个数据结构,一个文件等.也就是资源每一次,同一个时间只能被一个线程处理.
简单的讲, CRITICAL_SECTION 用来保护资源, 当一个线程对某资源A写的时候, 避免别的线程对资源A进行任何操作. 就是保护数据的一致性.
你刚刚用的在同一线程. 没有用...
多线程才使用..
InitializeCriticalSection( )变量后,不管多少次EnterCriticalSection( )都能进去
调试很久不知道是哪里的问题,写了个最简单的测试程序(win32控制台程序,使用mfc库),
还是有问题.测试程序如下
#include "stdafx.h"
#include "testLock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CWinApp theApp;
using namespace std;
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
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}
CRITICAL_SECTION Sec;
InitializeCriticalSection( &Sec );
EnterCriticalSection( &Sec ); ///能调用返回,返回后
///Sec.LockCount=0,RecursionCount=1
EnterCriticalSection( &Sec ); ///还是能返回,没招,返回后
///Sec.LockCount=1,RecursionCount=2
return nRetCode;
}
有经验的大侠指点下,不胜感激~~
---------------------------------------------------------------
CRITICAL_SECTION CriticalSection;
// Initialize the critical section.
InitializeCriticalSection(&CriticalSection);
// Request ownership of the critical section.
__try
{
EnterCriticalSection(&CriticalSection);
// Access the shared resource.
}
__finally
{
// Release ownership of the critical section.
LeaveCriticalSection(&CriticalSection);
// Release resources used by the critical section object.
DeleteCriticalSection(&CriticalSection)
}
---------------------------------------------------------------
都不理解CRITICAL_SECTION 用来什么的.
所谓CRITICAL_SECTION 是用来处理"一份被共享的资源", 如一段内存, 一个数据结构,一个文件等.也就是资源每一次,同一个时间只能被一个线程处理.
简单的讲, CRITICAL_SECTION 用来保护资源, 当一个线程对某资源A写的时候, 避免别的线程对资源A进行任何操作. 就是保护数据的一致性.
你刚刚用的在同一线程. 没有用...
多线程才使用..
相关文章推荐
- 一个简单的0-1背包问题(1)
- 一个简单的Python问题——值到底是谁?
- 一个简单的apache rewrite问题
- 问题:关于贴友的一个书本页面简单布局(html+css)的实现
- 一个简单的跨库事务问题
- i++不是原子操作,看似简单,实则巨坑的一个线程同步的问题。synchronized 和 volatile
- 记录一个简单的问题
- 解决TCP协议粘包问题的一个简单方案
- 发一个非常简单的效果,是在浏览器里面上下左右都居中的问题
- RF框架-一个简单的脚本实现及元素定位不到问题的解决
- Pro*C编程初步 + 栽在了一个非常简单的问题上
- (集合和引用类型、基本数据类型赋值不一样)一个简单的java问题 先后的赋值问题
- 一个简单的递归求目录的程序遇到的问题
- 记一个简单的小问题 win10 java环境搭建问题
- 水杯问题(就是一个简单的广搜)
- win编程的一个问题求教:
- 一个简单的问题 我又机智的解决了额
- 【内存管理-问题实例】为什么执行了numberOfRowsInSection方法后,dataArray的retainCount就少了一个? 谁把它 release了?
- 如何交换a,b的数值——一个简单的问题就证明现在的你依然弱爆了
- 求助一个问题,100分,我也没多少分了。