C++11线程指南(1)--创建线程

来源:互联网 发布:侧吸好还是顶吸好 知乎 编辑:程序博客网 时间:2024/06/02 09:38

1. 创建线程

#include<iostream>#include<thread>using namespace std;void thread_func(){cout<<"thread function\n"<<endl;}int main(){std::thread thr(&thread_func);cout<<"main thread"<<endl;thr.join();return 0;}
  运行结果:
  main thread
  thread function
 
  首先,创建一个thread 对象(工作线程). 
  主线程使用join来等待工作线程结束。如果不等待的话,主线程会一直运行到程序结束,工作线程可能都来不及执行。
  程序运行流程大体如下:
  
  然而,真实运行情况可能未必如此对称。也许像下面这样:
  
  为何?因为工作线程是依靠构造函数std::thread thr来启动的,会有一个时间上的开销(这个开销可以使用thread pool来减少)。此时,主线程可能阻塞在虚线所示的各个地方。

2. 分离线程(Detach Thread)

  基于上面实现,我们可以使一个线程变为daemon process. 
int main(){std::thread thr(&thread_func);cout<<"main thread"<<endl;//thr.join();thr.detach();return 0;}
  一旦一个线程被detach之后,就不能再让它join到主线程了。因此,下面的这段代码是错误的,并且会引起程序crash.
int main(){std::thread thr(&thread_func);cout<<"main thread"<<endl;//thr.join();thr.detach();        thr.join();return 0;}
  一旦一个线程被detach后,它会永远存活下去。
  为了避免程序crash, 可以使用joinable来进行判断,如下所示:
int main(){std::thread thr(&thread_func);cout<<"main thread"<<endl;if(thr.joinable())thr.join();return 0;}

3. Callable Objects

  前面的例子中,都是以常规函数做为线程载体。其实可以使用任何可以被调用的对象做为载体,如lambda表达式,或者函数对象(functor). 
#include<iostream>#include<thread>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()));

4. 传递参数给线程

  下面是一个传递string参数的例子:
#include<iostream>#include<thread>#include<string>void thread_func(std::string str){std::cout<<"thread function ";std::cout<<"message is = "<<str<<std::endl;}int main(){std::string str = "hello world";std::thread thr(&thread_func, std::ref(str));std::cout<<"main thread message = "<<str<<std::endl;thr.join();return 0;}
  运行结果为:
  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 =

5. 获取线程ID

  可用如下方法获取线程的ID:
int main(){std::string str = "hello world";std::thread thr(&thread_func, std::move(str));std::cout<<"main thread message = "<<str<<std::endl;std::cout<<"main thread id = "<<std::this_thread::get_id()<<std::endl;std::cout<<"child thread id = "<<thr.get_id()<<std::endl;thr.join();getchar();return 0;}
  运行结果为:
  main thread id = 7564
  child thread id = 6452

6. 线程数量

int main(){    std::cout << "Number of threads = "<<  std::thread::hardware_concurrency() << std::endl;    return 0;}
  运行结果为:
  Number of threads = 4

7. Lambda函数

  因为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

0 0
原创粉丝点击