走过,路过,不要错过!!!
下面这段代码中 创建了线程然后就关掉,这样有啥用啊?线程函数根本就不可能执行啊!!!看不明白,请大虾们帮忙解释一下!!!
for(i = 0; i < SystemInfo.dwNumberOfProcessors * 2; i++)
{
HANDLE ThreadHandle;
// Create a server worker thread and pass the completion port to the thread.
if ((ThreadHandle = CreateThread(NULL, 0, ServerWorkerThread, CompletionPort,
0, &ThreadID)) == NULL)
{
printf("CreateThread() failed with error %d\n", GetLastError());
return;
}
// Close the thread handle
CloseHandle(ThreadHandle);
}
问题点数:20、回复次数:4Top
1 楼seaquester()回复于 2005-01-24 11:35:32 得分 0
只是 CloseHandle 是不会停止线程的Top
2 楼mxk130((大大的小新))回复于 2005-01-24 12:10:01 得分 0
#include<iostream.h>
#include<windows.h>
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
cout<<"haha"<<endl;
for(int i=0;i<10;i++)
cout<<i<<endl;
return i;
}
void main()
{
HANDLE ht=CreateThread(NULL,0,ThreadProc,NULL,0,NULL);
if (ht=NULL)
cout<<"创建线程失败"<<endl;
else
cout<<"成功"<<endl;
// Sleep(300);
CloseHandle(ht);
}
可是这段代码证明:
线程函数没有机会运行啊Top
3 楼netsys2(来电!)回复于 2005-01-24 12:48:08 得分 0
for(i = 0; i < SystemInfo.dwNumberOfProcessors * 2; i++)
---->SystemInfo.dwNumberOfProcessors:
显然该代码是用来测试多CPU下各CPU处理线程的情况,
所以创建完后就关闭,但在CPU的使用窗口上可以看到是哪个CPU在工作,占用率是多少。Top
4 楼seaquester()回复于 2005-01-24 17:36:39 得分 20
看不到线程print的信息只是因为主进程结束的太快了,把Sleep(300)前面的“//”去掉就可以看到线程执行了。如下:
#include<iostream>
#include<windows.h>
using namespace std;
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
cout<<"haha"<<endl;
for(int i=0;i<10;i++)
cout<<i<<endl;
return i;
}
void main()
{
HANDLE ht=CreateThread(NULL,0,ThreadProc,NULL,0,NULL);
if (ht=NULL)
cout<<"创建线程失败"<<endl;
else
cout<<"成功"<<endl;
CloseHandle(ht);
Sleep(300);
cout<<"程序结束"<<endl;
}
Top




