CreateThread成功后,紧接CloseHandle,不解
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被删掉了!
那么这么做有什么意义呢?帮忙解释,谢谢!