CreateThread x3000返回stdout句柄

时间:2017-12-17 14:53:13

标签: c winapi

我在创建数千个线程并关闭它时遇到问题。 看看这段代码:

HANDLE threadHandles[i];
for(int i = 0; i < 1000; i++)
{
    CreateThread(0, 0, &func, 0, CREATE_SUSPENDED, threadHandles[i]);
    printf("%i - %i\n", 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 - %i\n", 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

问题出在哪里?为什么创建的句柄和现有标准句柄之间存在冲突?

1 个答案:

答案 0 :(得分:3)

函数返回线程句柄!最后一个参数接收线程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 %u\n", GetLastError());
        break;
    }
    printf("Thread #%u: handle=%p id=%u\n", 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