创建新线程(C、Windows)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1456435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a new thread (C, Windows)
提问by wonderer
OK, I'm a bit confused here. The following code works:
好的,我在这里有点困惑。以下代码有效:
HANDLE CreateSideThread()
{
DWORD dwGenericThread;
HANDLE hThread1 = CreateThread(NULL, 0, CallBackFunc, NULL, 0, &dwGenericThread);
return hThread1;
}
int main()
{
HANDLE Thread1;
Thread1 = CreateSideThread();
WaitForSingleObject(hThread1, INFINITE);
SomeOtherFunction();
return 0;
}
The program does other things but you get the idea. It basically creates a new thread and executes whatever it is in CallBackFunc (which is an endless loop that check db status). Now, if I remove WaitForSingleObject() then the program will not even try CallBackFunc once and execute SomeOtherFunction(). What's the point then of a thread? I mean, i'm confused here.
该程序会做其他事情,但你明白了。它基本上创建一个新线程并执行它在 CallBackFunc 中的任何内容(这是一个检查数据库状态的无限循环)。现在,如果我删除 WaitForSingleObject() 那么程序甚至不会尝试 CallBackFunc 一次并执行 SomeOtherFunction()。那么线程有什么意义呢?我的意思是,我在这里很困惑。
What I am trying to do is call that thread with the check for the database status and keep that thread going while I continue with my program, calling other functions.
我想要做的是调用该线程并检查数据库状态,并在我继续执行程序时保持该线程运行,调用其他函数。
What am I doing wrong? Please post a sample snippet.
我究竟做错了什么?请发布示例代码段。
Thanks
谢谢
回答by Greg Hewgill
Without the WaitForSingleObject
, your call to SomeOtherFunction()
probably returns quickly enough for the program to exit before the new thread even gets a chance to run once.
如果没有WaitForSingleObject
,您的调用SomeOtherFunction()
可能会很快返回,以便程序在新线程有机会运行一次之前退出。
When a C program returns from its main()
function, the runtime system calls exit()
for you. This forcibly exits your program even if other threads are trying to run at the same time. This is in contrast to other languages like Java for example, where exiting the main thread will not exit the process until all other (non-daemon) threads have finished running too.
当 C 程序从其main()
函数返回时,运行时系统会调用exit()
您。即使其他线程试图同时运行,这也会强制退出您的程序。这与其他语言(例如 Java)形成对比,其中退出主线程不会退出进程,直到所有其他(非守护进程)线程也完成运行。
回答by DarkSquid
Threads are typically used for doing background work and freeing up the calling thread to do other things.
线程通常用于做后台工作并释放调用线程来做其他事情。
Normally, the behavior you describe (calling SomeOtherFunction()) is exactly what you'd want: I am going to kick of a background 'job' and go about my life.
通常,您所描述的行为(调用 SomeOtherFunction())正是您想要的:我将开始从事后台“工作”并开始我的生活。
It would appear that your example is just fine - though if you merely return from main() your thread will of course terminate (as it's owned by the parent process).
看起来你的例子很好 - 尽管如果你只是从 main() 返回,你的线程当然会终止(因为它归父进程所有)。
Maybe some more detail on why what you're doing isn't what you expect to happen?
也许有更多关于为什么你正在做的事情不是你期望发生的事情的细节?
回答by gbjbaanb
What you're finding is that the main thread completes before you notice CallbackFunc is called. When you have the Wait call in, the main thread is blocked until the new thread finishes and so you see the thread func being executed.
您发现主线程在您注意到 CallbackFunc 被调用之前完成。当您调用 Wait 时,主线程将被阻塞,直到新线程完成,因此您会看到正在执行的线程函数。
Threads are not as cheap as you think, if you replace the SomeOtherFunction with something that takes a long enough time to run, you'll see your thread function being called even without the Wait call.
线程并不像您想象的那么便宜,如果您将 SomeOtherFunction 替换为运行时间足够长的东西,即使没有 Wait 调用,您也会看到您的线程函数被调用。
回答by RED SOFT ADAIR
CallBackFunc will of course be called, but there is no guarantee, when your stared threads will be up and running. They willbe working once, but its unpredictable when the start doing so. Thats the job and property of the systems scheduler. In your case they do not anything when the second function is already called.
CallBackFunc 当然会被调用,但不能保证您的关注线程何时启动并运行。他们将工作一次,但在开始时是不可预测的。这就是系统调度程序的工作和属性。在你的情况下,当第二个函数已经被调用时,它们什么都不做。