C++11之线程库thread


CMake解决c++11的phread库问题:undefined reference to `pthread_create’


一、资料

C++11并发之std::thread
C++官方thread库文档

二、一个简单的程序

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread

using namespace std;
 
void foo() 
{
	// do stuff...
	for( int i = 0 ;i < 10 ; i++ )
	{
		cout << "foo:" << i << endl;
	}
}

void bar(int x)		// 虽然传入了,但我代码没用到,演示而已
{
	// do stuff...
	for( int i = 0 ;i < 10 ; i++ )
	{
		cout << "bar:" << i << endl;
	}
}


int main() 
{
	cout << "before spawn new thread, must be main...\n";
	cout << "before spawn new thread, must be main...\n";
	cout << "before spawn new thread, must be main...\n";

	thread first (foo);     // spawn new thread that calls foo()
	thread second (bar,0);  // spawn new thread that calls bar(0)

	cout << "main, foo and bar now execute concurrently...\n";
	cout << "main, foo and bar now execute concurrently...\n";
	cout << "main, foo and bar now execute concurrently...\n";

	// synchronize threads:
	first.join();                // pauses until first finishes
	second.join();               // pauses until second finishes

	cout << "after join thread, must be main...\n";
	cout << "after join thread, must be main...\n";
	cout << "after join thread, must be main...\n";

	return 0;
}
/*
before spawn new thread, must be main...
before spawn new thread, must be main...
before spawn new thread, must be main...
foo:0
main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
foo:1
foo:2
foo:3
foo:4
foo:5
foo:6
foo:7
foo:8
foo:9
bar:0
bar:1
bar:2
bar:3
bar:4
bar:5
bar:6
bar:7
bar:8
bar:9
after join thread, must be main...
after join thread, must be main...
after join thread, must be main...
*/

1.创建线程

  • 线程要做的事情:用一个返回值为void的函数func来实现
    函数的参数为要传入函数的东西

  • 线程的创建

thread thread_name(func[,args])

第一个参数是函数名(不带括号),之后的参数就是你想传入的自定义参数
有的写法&func是函数名前加&,其实没有必要,但也对。

  • 数组写法
thread threads[THREAD_SIZE];
threads[i]=thread(func);
threads[i].join();
  • 新的线程创建后,在被join()之前,是和主线程混合在一起并发执行的。

2.join的作用

  • join阻塞主线程,让主线程一直等待停在join处,直到其新创建的线程执行完毕后才会返回主线程继续下去。

  • join会清理子线程相关的内存空间,避免造成资源的泄露,所以该方法只能被调用一次。

  • 必须保证该方法被有效执行,不会因程序异常而被跳过:c++11中关于std::thread的join的思考

3.detach

  • detach()函数会让线程在后台运行,即说明主线程不会等待子线程运行结束才结束

通常称分离线程为守护线程(daemon threads),UNIX中守护线程是指,没有任何显式的用户接口,并在后台运行的线程。这种线程的特点就是长时间运行;线程的生命周期可能会从某一个应用起始到结束,可能会在后台监视文件系统,还有可能对缓存进行清理,亦或对数据结构进行优化。另一方面,分离线程的另一方面只能确定线程什么时候结束,发后即忘(fire andforget)的任务就使用到线程的这种方式。

// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread

using namespace std;
 
void foo() 
{
	// do stuff...
	for( int i = 0 ;i < 10 ; i++ )
	{
		cout << "foo:" << i << endl;
	}
}

void bar(int x)
{
	// do stuff...
	for( int i = 0 ;i < 10 ; i++ )
	{
		cout << "bar:" << i << endl;
	}
}


int main() 
{
	cout << "before spawn new thread, must be main...\n";

	thread first (foo);     // spawn new thread that calls foo()
	thread second (bar,0);  // spawn new thread that calls bar(0)

	cout << "main, foo and bar now execute concurrently...\n";
	cout << "main, foo and bar now execute concurrently...\n";
	cout << "main, foo and bar now execute concurrently...\n";

	// synchronize threads:
	first.detach();
	second.detach();

	cout << "after detach thread, main return.\n";

	return 0;
}
/*
before spawn new thread, must be main...
foo:main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
main, foo and bar now execute concurrently...
after detach thread, main return.
0
*/

参考:
C++11多线程join()和detach()的理解

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值