CreateThread成功后,紧接CloseHandle,不解

kaphoon 2005-11-13 01:20:23
SDK的文档中,
Multithreaded Pipe Server那节有个例子.

fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

if (fConnected)
{
// Create a thread for this client.
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
(LPTHREAD_START_ROUTINE) InstanceThread,
(LPVOID) hPipe, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID

if (hThread == NULL)
{
printf("CreateThread failed");
return 0;
}
else CloseHandle(hThread);
}
else
// The client could not connect, so close the pipe.
CloseHandle(hPipe);
}


在成功创建一个线程后就调用CloseHandle(hThread).
hThread在创建后,计数为1,CloseHandle后,计数为0,也就是说hThread被删掉了!
那么这么做有什么意义呢?帮忙解释,谢谢!

...全文
505 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kaphoon 2005-11-13
  • 打赏
  • 举报
回复
原来如此啊!
揭帖
DentistryDoctor 2005-11-13
  • 打赏
  • 举报
回复
CloseHandle后线程仍然会正常执行,但却不能操控它了。
vc_asm 2005-11-13
  • 打赏
  • 举报
回复
CreateThread启动了一个线程,同时产生一个句柄让你好操纵这个线程,如果你不要用这个句柄了就CloseHandle关掉它.不要想多了

15,471

社区成员

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

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