这是我用来启动线程的方法并且它有效,但我想知道这种方式是否有任何缺点。
void myFunc()
{
//code here
}
unsigned int _stdcall ThreadFunction(void* data)
{
myFunc();
return 0;
}
我使用的主要功能是:
HANDLE A = (HANDLE)_beginthredex(0,0,&ThreadFunction,0,0,0);
我用CloseHandle(A);
结束了帖子。
答案 0 :(得分:8)
If you have access to C++11 use the <thread>
library and you won't need to worry about cross-platform compatibility:
#include <thread>
std::thread t(&ThreadFunction, nullptr);
To wait for the thread's execution to finish, use join()
:
t.join();
This blocks until the function that the thread is supposed to run has returned.
Otherwise, use CreateThread (since it looks like you're on Windows) or beginthreadex.
For POSIX, use pthread_create()
.