博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++11线程指南(1)--创建线程
阅读量:4071 次
发布时间:2019-05-25

本文共 2340 字,大约阅读时间需要 7 分钟。

目录


1. 创建线程

#include
#include
using namespace std;void thread_func(){ cout<<"thread function\n"<

  运行结果:

  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"<

  一旦一个线程被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"<

3. Callable Objects

  前面的例子中,都是以常规函数做为线程载体。其实可以使用任何可以被调用的对象做为载体,如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()));

4. 传递参数给线程

  下面是一个传递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 =

5. 获取线程ID

  可用如下方法获取线程的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 = 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

你可能感兴趣的文章
UIButton添加block点击事件
查看>>
利用runtime给类别添加属性
查看>>
iOS使用支付宝支付步骤
查看>>
本地推送
查看>>
远程推送
查看>>
访问系统相册
查看>>
FMDB的使用
查看>>
UIImage存为本地文件与UIImage转换为NSData
查看>>
通知的使用
查看>>
KVC与KVO
查看>>
NSThread
查看>>
NSOperation
查看>>
GCD
查看>>
coreData的使用
查看>>
URL里汉字转码
查看>>
NSURLConnection的简单使用
查看>>
对NSURLConnection的简单封装
查看>>
对大文件的断点续传
查看>>
JSON和XML
查看>>
XML解析
查看>>