C++ 如何在VC++中创建线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/505261/
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
How to create threads in VC++
提问by Aaron
I tend to use POSIX Threads, when programming in C, under Linux.
Without MFC
在Linux下用 C 编程时,我倾向于使用POSIX 线程。
没有MFC
Question:
题:
How would I then create threads in VC++?
那么我将如何在 VC++ 中创建线程?
Find more information on threads under win32?
查找有关 win32 下线程的更多信息?
Edit:
编辑:
- Brief illustrations
- 简要插图
I LOVE stackoverflow - best resource for students!
我喜欢 stackoverflow - 学生的最佳资源!
Regards
问候
采纳答案by Michael Burr
You should not use the raw Win32 CreateThread()
API.
您不应使用原始 Win32 CreateThread()
API。
Use the C runtime's _beginthreadex()
so the runtime has an opportunity to set up its own thread support.
使用 C 运行时,_beginthreadex()
这样运行时就有机会设置自己的线程支持。
回答by galets
if you're looking for a platform-independent method, use boost
如果您正在寻找与平台无关的方法,请使用boost
there's also beginthread() and beginthreadex() functions. Both seem to be supplemental to Win32 API, in a sense that in many use cases, you still need to call some Win32 functions (such as CloseHandle for beginthreadex). So, if you don't care that much about platform compatibility, you might as well cut the foreplay and use CreateThread().
还有 beginthread() 和 beginthreadex() 函数。两者似乎都是对Win32 API的补充,从某种意义上说,在很多用例中,你仍然需要调用一些Win32函数(比如beginthreadex的CloseHandle)。所以,如果你不太关心平台兼容性,你不妨剪掉前戏并使用 CreateThread()。
Win32 thread handling is documented here: http://msdn.microsoft.com/en-us/library/ms684852(VS.85).aspx
此处记录了 Win32 线程处理:http: //msdn.microsoft.com/en-us/library/ms684852(VS.85).aspx
[edit1] example:
[edit1] 示例:
DWORD WINAPI MyThreadProc( void* pContext )
{
return 0;
}
HANDLE h = CreateThread( NULL, 0, MyThreadProc, this, 0L, NULL );
WaitForSingleObject(h, TIME); // wait for thread to exit, TIME is a DWORD in milliseconds
[edit2] CRT & CreateThread():
[edit2] CRT & CreateThread():
per MSDN:
每 MSDN:
A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.
调用 C 运行时库 (CRT) 的可执行文件中的线程应该使用 _beginthreadex 和 _endthreadex 函数进行线程管理,而不是使用 CreateThread 和 ExitThread;这需要使用 CRT 的多线程版本。如果使用 CreateThread 创建的线程调用 CRT,则 CRT 可能会在内存不足的情况下终止进程。
回答by Hans Passant
You can use either the CRT function _beginthreadex() or the Windows API function CreateThread(). _beginthreadex() is required for early versions of VC++ that had a CRT that didn't lazily initialize thread-local storage. CreateThread() is fine in at least VS2005 and up.
您可以使用 CRT 函数 _beginthreadex() 或 Windows API 函数 CreateThread()。_beginthreadex() 对于早期版本的 VC++ 是必需的,这些版本的 CRT 不会延迟初始化线程本地存储。CreateThread() 至少在 VS2005 及更高版本中很好。
回答by Andy
You probably want to take a look at the CreateThread()function.
您可能想看看CreateThread()函数。
回答by ConcernedOfTunbridgeWells
Some good books on the subject are Petzold's Programming Windowsand Richter's Programming Applications for Windows.In particular, the latter goes into server side programming such as thread and synchronisation API's in a lot of depth.
关于这个主题的一些好书是 Petzold 的Windows 编程和 Richter 的Windows 编程应用程序。特别是,后者深入研究了服务器端编程,例如线程和同步 API。
EDIT:For code snippets, Google is your friend. For example This articlehas some minimal thread examples.
编辑:对于代码片段,谷歌是你的朋友。例如本文有一些最小的线程示例。
回答by Adam Rosenfield
Use _beginthread()
or _beginthreadex()
to create a new thread. Do NOT use the Win32 function CreateThread()
-- it does not properly initialize the multithreaded aspects of the C runtime. See also this question.
使用_beginthread()
或_beginthreadex()
来创建一个新线程。不要使用 Win32 函数CreateThread()
——它没有正确初始化 C 运行时的多线程方面。另请参阅此问题。
回答by Philibert Perusse
There is also the _beginthread()function you can look up. It differs a bit from CreateThread(), you should be aware of the differences before choosing one.
也有_beginthread() ,你可以查找功能。它与 CreateThread() 略有不同,您应该在选择之前了解这些差异。