【发布时间】:2014-01-29 22:14:58
【问题描述】:
到目前为止,我只在 Linux 平台上使用过进程和线程。 现在我尝试在 Windows 上移动。我立即停止了非常简单的程序。 你能告诉我,如果我用 getch 删除那行,为什么我的程序什么都不写吗? 我希望我的线程在我不按任何东西的情况下完成。 提前谢谢你
#include <windows.h>
#include <stdio.h>
DWORD WINAPI ThreadFunc()
{
printf("lets print something");
return 0;
}
VOID main( VOID )
{
DWORD dwThreadId;
HANDLE hThread;
hThread = CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadFunc, // thread function
NULL, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
// Check the return value for success.
if (hThread == NULL)
{
printf( "CreateThread failed (%d)\n", GetLastError() );
}
else
{
_getch();
CloseHandle( hThread );
}
}
【问题讨论】:
-
如果你在 VS 中运行程序,控制台窗口可能会立即消失。通过提供 _getch(),控制台窗口将等到您点击 _getche()。
-
是的,我的第一个想法。然后我在终端用 gcc 试了一下。没用。
-
那么您的主线程可能在创建另一个线程之前已经关闭。因此,您无法看到该消息。
标签: c multithreading