谁能帮忙解释一下为什么

palm_m 2012-07-06 10:34:06
如下是我的程序,我期望程序输出 执行 printf("Thread 2\n");,但是程序一直执行不到该句,为什么?

#include <Windows.h>
DWORD WINAPI Thread1(LPVOID);
DWORD WINAPI Thread2(LPVOID);


CRITICAL_SECTION criticalsection;

DWORD WINAPI Thread1(void* parm)
{
if(TryEnterCriticalSection(&criticalsection))
{
printf("Thread 1 ok\n");
}
else
{
printf("Therad 1 error\n");
}
EnterCriticalSection(&criticalsection);
Sleep(1000*1);
printf("Thread 1\n");
LeaveCriticalSection(&criticalsection);
return 0;
}
DWORD WINAPI Thread2(void* parm)
{
if(TryEnterCriticalSection(&criticalsection))
{
printf("Thread 2 ok\n");
}
else
{
printf("Therad 2 error\n");
}
EnterCriticalSection(&criticalsection);
Sleep(1000*1);
printf("Thread 2\n");
LeaveCriticalSection(&criticalsection);
return 0;
}

int main()
{
HANDLE handl;
DWORD id;
InitializeCriticalSection(&criticalsection);
handl = CreateThread(NULL, NULL, Thread1, NULL, 0, &id);
//Sleep(1000*2);
handl = CreateThread(NULL, NULL, Thread2, NULL, 0, &id);
//main function wait
while (1)
{
}
return 0;
}
...全文
109 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
palm_m 2012-07-09
  • 打赏
  • 举报
回复
的确如你所说,需要在Thread1和Thread的TryEnterCriticalSection(&criticalsection)分支加入LeaveCriticalSection(&criticalsection),加入后,程序是我预期的输出。
并且你的解释也很正确,3q
dahaiI0 2012-07-09
  • 打赏
  • 举报
回复
上面确实没说调用TryEnterCriticalSection后必须调用LeaveCriticalSetion。

先看下这个函数的作用申明:This function attempts to enter a critical section without blocking,If the call is successful, the calling thread takes ownership of the critical section.

然后看1楼里有一句话
The thread must call LeaveCriticalSection once for each time that it entered the critical section.
根据返回值,如果是true就表明进入了critical section,这时候就要调用leavecritical,如果是false就不需要了

看注释文档什么的别纠结字眼,联系下上下文。
palm_m 2012-07-09
  • 打赏
  • 举报
回复
上一段英文我仔细的看了,文中没有提到在TryEnterCriticalSection调用之后必须调用LeaveCriticalSetion。
dahaiI0 2012-07-06
  • 打赏
  • 举报
回复
DWORD WINAPI Thread1(void* parm)
{
if(TryEnterCriticalSection(&criticalsection))
{
printf("Thread 1 ok\n");
LeaveCriticalSection(&criticalsection);//注意看这里
}
else
{
printf("Therad 1 error\n");
}
EnterCriticalSection(&criticalsection);
Sleep(1000*1);
printf("Thread 1\n");
LeaveCriticalSection(&criticalsection);
return 0;
}

要多看下MSDN,特别是一个函数你不是很清楚它的用法,查下MSDN往往会让你有新的收获。回到正题
To enable mutually exclusive use of a shared resource, each thread calls the EnterCriticalSection or TryEnterCriticalSection function to request ownership of the critical section before executing any section of code that uses the protected resource. The difference is that TryEnterCriticalSection returns immediately, regardless of whether it obtained ownership of the critical section, while EnterCriticalSection blocks until the thread can take ownership of the critical section. When it has finished executing the protected code, the thread uses the LeaveCriticalSection function to relinquish ownership, enabling another thread to become the owner and gain access to the protected resource. The thread must call LeaveCriticalSection once for each time that it entered the critical section.
这段英文能看懂的话应该就知道问题所在了

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧