使用boost/thread:
#include <boost/thread.hpp>
void myFunc()
{
// several stuff
}
int main()
{
boost::thread thread(myFunc);
// thread created
//...
// I would like to run myFunc in a thread so that the code below could execute
// while it is being completed.
//...
// must wait for thread to finish before exiting
thread.join();
}
> g++ -lboost_thread test.cpp
您将需要确保升压线程库已经建成。
使用pthreads:
void* myFunc(void* arg)
{
// several stuff
return NULL;
}
int main()
{
pthread_t thread;
int result = pthread_create(&thread, NULL, myFunc, NULL);
if (result == 0)
{
// thread created
//...
// I would like to run myFunc in a thread so that the code below could execute
// while it is being completed.
//...
// must wait for thread to finish before exiting
void* result;
pthread_join(thread, &result);
}
}
> g++ -lpthread test.cpp
线程依赖于操作系统。在不知道操作系统的情况下,我们只能建议您查看一些与平台无关的库,如boost和Qt。 – Juliano
需要一个库或C++ 11 –
对不起,我的错误,Windows 7 – Pumpkin