我有问题创建数千个线程并关闭它。查看此代码:
HANDLE threadHandles[i];
for(int i = 0; i < 1000; i++)
{
CreateThread(0, 0, &func, 0, CREATE_SUSPENDED, threadHandles[i]);
printf("%i - %in", i, threadHandles[i]);
CloseHandle(threadHandles[i])
}
printf("Last Error is %i", GetLastError());
它应该继续进行此输出:
0 - 236423
1 - 23456236
2 - 2373547
3 - 73521346
4 - 23456775
5 - 78543683465
...
2998 - 754752
2999 - 23462346
Last Error is 0
喜欢。
但实际上它不会打印。为什么?因为其中一个线程与Stdout句柄发生冲突。我使用此代码意识到它:
HANDLE stdout = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE threadHandles[i];
for(int i = 0; i < 1000; i++)
{
CreateThread(0, 0, &func, 0, CREATE_SUSPENDED, threadHandles[i]);
printf("%i - %in", i, threadHandles[i]);
if(threadHandles[i] != stdout) // I have stdout == 12 on my machine
CloseHandle(threadHandles[i])
}
这样可以工作:
0 - 236423
1 - 23456236
2 - 2373547
3 - 0
4 - 23456775
5 - 78543683465
...
2998 - 0
2999 - 23462346
Last Error is 6
问题在哪里?为什么创建的手柄和现有标准手柄之间存在冲突?
函数返回线程句柄!最后一个参数接收线程ID,而不是句柄。
HANDLE threadHandles[1000] = { 0 };
UINT i;
for (i = 0; i < 1000; ++i)
{
DWORD threadId;
threadHandles[i] = CreateThread(0, 0, &func, 0, /*CREATE_SUSPENDED*/0, &threadId);
if (!threadHandles[i])
{
printf("Failed to create thread, error %un", GetLastError());
break;
}
printf("Thread #%u: handle=%p id=%un", i, threadHandles[i], threadId);
// Not closing the handle here so the example will show unique thread handles and ids: CloseHandle(threadHandles[i]), threadHandles[i] = NULL;
}
// TODO: Wait for threads or do some other work?
for (i = 0; i < 1000; ++i)
if (threadHandles[i])
CloseHandle(threadHandles[i]);
在我的计算机上,这印刷
Thread #0: handle=0000002C id=16424
Thread #1: handle=00000030 id=21192
Thread #2: handle=00000034 id=21180
Thread #3: handle=00000038 id=17336
Thread #4: handle=0000003C id=21184
Thread #5: handle=00000040 id=4460
...
Thread #991: handle=00000FE8 id=12280
Thread #992: handle=00000FEC id=20360
Thread #993: handle=00000FF0 id=20328
Thread #994: handle=00000FF4 id=16060
Thread #995: handle=00000FF8 id=4556
Thread #996: handle=00000FFC id=20296
Thread #997: handle=00001004 id=10316
Thread #998: handle=00001008 id=20604
Thread #999: handle=0000100C id=20264