本文共 2340 字,大约阅读时间需要 7 分钟。
目录
#include#include using namespace std;void thread_func(){ cout<<"thread function\n"<
运行结果:
main thread thread function首先,创建一个thread 对象(工作线程).
主线程使用join来等待工作线程结束。如果不等待的话,主线程会一直运行到程序结束,工作线程可能都来不及执行。 程序运行流程大体如下: 然而,真实运行情况可能未必如此对称。也许像下面这样: 为何?因为工作线程是依靠构造函数std::thread thr来启动的,会有一个时间上的开销(这个开销可以使用thread pool来减少)。此时,主线程可能阻塞在虚线所示的各个地方。基于上面实现,我们可以使一个线程变为daemon process.
int main(){ std::thread thr(&thread_func); cout<<"main thread"<
一旦一个线程被detach之后,就不能再让它join到主线程了。因此,下面的这段代码是错误的,并且会引起程序crash.
int main(){ std::thread thr(&thread_func); cout<<"main thread"<
一旦一个线程被detach后,它会永远存活下去。
为了避免程序crash, 可以使用joinable来进行判断,如下所示:int main(){ std::thread thr(&thread_func); cout<<"main thread"<
前面的例子中,都是以常规函数做为线程载体。其实可以使用任何可以被调用的对象做为载体,如lambda表达式,或者函数对象(functor).
#include#include class Functor {public: void operator() () { std::cout<<"functor\n"; }};int main(){ Functor fnc; std::thread thr(fnc); std::cout<<"main thread\n"; thr.join(); return 0;}
运行结果:
functor main thread 或者,也可以传递一个临时对象:// Functor fnc;std::thread thr((Functor()));
下面是一个传递string参数的例子:
#include#include #include void thread_func(std::string str){ std::cout<<"thread function "; std::cout<<"message is = "< <
运行结果为:
main thread message = hello world thread function message is = hello world 如果想传入的参数为一个引用,我们自然会想到下面的方法: void thread_func(std::string &str) 但是,仅仅这样是不够的,还需要在thread构造函数中,传入std::ref。 得到结果为: thread function message is = hello world main thread message = hi 此外,还有另外的方法,即没有参数的复制,也没有参数之间的内存共享。它就是std::move()。 std::thread thr(&thread_func, std::move(str)); 运行结果为: thread function message is = hello world main thread message =可用如下方法获取线程的ID:
int main(){ std::string str = "hello world"; std::thread thr(&thread_func, std::move(str)); std::cout<<"main thread message = "<<
运行结果为:
main thread id = 7564 child thread id = 6452int main(){ std::cout << "Number of threads = "<< std::thread::hardware_concurrency() << std::endl; return 0;}
运行结果为:
Number of threads = 4因为C++支持lambda函数,故可以使用它改写thread函数:
int main(){ std::thread thr([] () { std::cout<<"thread function\n"; }); std::cout<<"main thread\n"; thr.join(); return 0;}
运行结果为:
main thread thread function