C++多线程初探

学习使用thread构造函数

从thread定义中(有些地方看不太懂,错了还望指正)

1 无参构造

	thread() _NOEXCEPT
		{	// construct with no thread
		_Thr_set_null(_Thr);
		}
没有参数的构造thread()   noexceppt 创建空thread对象

-----------------------------------------------------------------------------
2右值引用参数

thread(thread&& _Other) _NOEXCEPT
		: _Thr(_Other._Thr)
		{	// move from _Other
		_Thr_set_null(_Other._Thr);
		}

支持 右值引用参数 复制

-----------------------------------------------------------------------------

3普通引用拷贝构造(不支持)

thread(const thread&) = delete;
无法使用

-----------------------------------------------------------------------------

4普通初始化构造函数

template<class _Fn,
		class... _Args,
		class = typename enable_if<
			!is_same<typename decay<_Fn>::type, thread>::value>::type>
		explicit thread(_Fn&& _Fx, _Args&&... _Ax)
		{	// construct with _Fx(_Ax...)
		_Launch(&_Thr,
			_STD make_unique<tuple<decay_t<_Fn>, decay_t<_Args>...> >(
				_STD forward<_Fn>(_Fx), _STD forward<_Args>(_Ax)...));
		}
改构造函数创建线程,调用执行fx函数,args为fx参数

a thread example  改自:http://en.cppreference.com/w/cpp/thread/thread/thread

#include<iostream>
#include<utility>
#include<thread>
#include<chrono>
#include<functional>
#include<atomic>
#include<cstdio>
void f1(int n)
{
	for (int i = 0; i < 5; ++i)
	{
		std::cout << "Threadf1 " <<n << "executing\n";
		std::this_thread::sleep_for(std::chrono::milliseconds(10));
	}
}
void f2(int &n)
{
	for (int i = 0; i < 5; ++i)
	{
		std::cout << "Thread f2 executing\n";
		++n;
		std::this_thread::sleep_for(std::chrono::microseconds(10));
	}
}
int main()
{
	int n = 0;
	std::thread t1;            //t1 is not a thread
	std::thread t2(f1, n + 1); //传值
	std::thread t3(f2, std::ref(n));  //传引用
	std::thread t4(std::move(t3));   //可以右值应用
	//std::thread t5(t2);   //不可被引用
	//t3.join();               // t4右值引用过 不可再被使用
	t2.join();
	t4.join();
	std::cout << "Final value of n is " << n << '\n';
}
另外=赋值

1支持右值赋值(上边拷贝也是)

	thread& operator=(thread&& _Other) _NOEXCEPT
		{	// move from _Other
		return (_Move_thread(_Other));
		}
2不支持普通的操作赋值(上边拷贝也是)

	thread& operator=(const thread&) = delete;

 关于赋值的例子

#include <stdio.h>
#include <stdlib.h>

#include <chrono>    // std::chrono::seconds
#include <iostream>  // std::cout
#include <thread>    // std::thread, std::this_thread::sleep_for

void thread_task(int n) {
	std::this_thread::sleep_for(std::chrono::seconds(n));
	std::cout << "hello thread "
		<< std::this_thread::get_id()
		<< " paused " << n << " seconds" << std::endl;
}

/*
* ===  FUNCTION  =========================================================
*         Name:  main
*  Description:  program entry routine.
* ========================================================================
*/
int main(int argc, const char *argv[])
{
	std::thread threads[5];
	std::cout << "Spawning 5 threads...\n";
	for (int i = 0; i < 5; i++) {
		threads[i] = std::thread(thread_task, i + 1);  //这里就是右值赋值thread& operator=(thread&& _Other) _NOEXCEPT
	}
	      /*threads[2] = threads[0];*/                    //这样是不对的
	std::cout << "Done spawning threads! Now wait for them to join\n";
	for (auto& t : threads) {
		t.join();
	}
	std::cout << "All threads joined.\n";

	return EXIT_SUCCESS;
}  /* ----------  end of function main  ---------- */



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值