CreateThread x3000 返回标准输出句柄

标签 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

像这样。

但实际上它不会打印任何内容。为什么?因为创建的线程之一与标准输出句柄发生冲突。 我使用这段代码实现了它:

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

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

最佳答案

函数返回线程句柄!最后一个参数接收线程 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

关于CreateThread x3000 返回标准输出句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856303/

相关文章:

c - unsetenv() 实现,是否需要释放内存?

c - 如何将 unsigned char 数组转换为 base64 字符串?

c - "while(*s++ = *t++)"如何复制一个字符串?

c++ - 将 Linux 兼容项目从 Windows 移植到 Linux

c - SetBkMode(hdc, TRANSPARENT) 不起作用

c# - 如何在 Process.StandardOutput 中禁用输出缓冲

C:按升序创建队列

c++ - TeamViewer 如何以编程方式在 Windows 上模拟 Ctrl-Alt-Del?

c++ - 在类中复制和存储 LPTSTR 会导致崩溃

python - 如何在 win7x64 上从 python 获取 PrivateUsage 内存值