并发与多线程笔记3

1 传递临时对象作为线程参数

#include <iostream>
#include <thread>
#include <string>

using namespace std;
void myprint(const int i, const string &pmybuf)
{
	cout << i << endl; // i并不是mavr的引用,实际上是值传递,那么我们认为主线程detach了子线程,子线程中也是安全的。
	cout << pmybuf.c_str() << endl;
	//这里是打印线程id
	//每个线程(主线程和子线程)实际上都对应着一个数字,不同线程对应不同数字
	// std::this_thread::get_id()来获得。
	cout << "threadid is " << std::this_thread::get_id() << endl;

	return;
}

int main()
{
	int mvar = 1;
	int &mvary = mvar;
	char mybuf[] = "this is a test!";
	//thread mytobj(myprint,mvar,mybuf);//但是mybuf到底是在什么时候转换成string的(主线程中还是主线程后)?
										//	主线程后会使子线程处于危险之中。事实上,存在,mybuf被回收后(main函数执行完毕),系统才用mybuf去转string的。
	thread mytobj(myprint, mvar, string(mybuf));// 这样就可以解决字符串引用带来的危险。
												//string(mybuf)保证了mybuf在主线程中转换为string类型后再传给子线程,这样detach时就不会出问题。
	mytobj.join();
	//mytobj.detach();// 子线程与主线程分别执行。
	cout << "I QUIT! \n" << endl;
	
	return 0;

}

2 传递类对象作为线程参数

#include <iostream>
#include <thread>
#include <string>

using namespace std;
//创建类
class A
{
public:
	//mutable int m_i;//mutable 表示即使是const类型的也可以修改它的值。
	int m_i;
	// 类型转换构造函数,可以把一个int类型转换成一个类A对象
	A(int a) :m_i(a) { cout << "[A::A(int a)构造函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl; }
	A(const A &a):m_i(a.m_i){ cout << "[A::A(const A)拷贝构造函数执行!] " << this << "threadid is " << std::this_thread::get_id() << endl; }
	~A(){ cout << "[A::A()析构函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl; }

	void thread_work(int num)
	{
		cout << "子线程 work 函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl;
	}
	void operator()(int num)
	{
		cout << "子线程 ()函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl;
	}
};
void myprint2(A &pmybuf)
{
	pmybuf.m_i = 199;//因为类型不是const了所以可以修改。
	cout << "子线程pmybuf的参数地址是" << &pmybuf << "threadid is " << std::this_thread::get_id() << endl;

	return;
}

//这个函数得对应mutable int m_i 语句
//void myprint2(const A &pmybuf)
//{
//	pmybuf.m_i = 199;//经验证,在此修改值不会影响到main函数
//	cout << "子线程pmybuf的参数地址是" << &pmybuf << "threadid is " << //std::this_thread::get_id() << endl;

//	return;
//}

int main()
{
	A myobj(10);
	//thread mytobj2(myprint2, myobj);//这得对应mutable int m_i 语句,不过这样运行语句,会发现myobj是被复制一份到达子线程的,这用detach时是不安全的。
	thread mytobj2(myprint2, std::ref(myobj));//std::ref()可以真正的将引用传入子线程
	mytobj2.join();
	cout << "主线程pmybuf的参数地址是" << &myobj << "threadid is " << 		std::this_thread::get_id() << endl;

	cout << "I QUIT! \n" << endl;
	
	return 0;

}

3 用成员函数指针做线程函数

#include <iostream>
#include <thread>
#include <string>

using namespace std;
//创建类
class A
{
public:
	//mutable int m_i;//mutable 表示即使是const类型的也可以修改它的值。
	int m_i;
	// 类型转换构造函数,可以把一个int类型转换成一个类A对象
	A(int a) :m_i(a) { cout << "[A::A(int a)构造函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl; }
	A(const A &a):m_i(a.m_i){ cout << "[A::A(const A)拷贝构造函数执行!] " << this << "threadid is " << std::this_thread::get_id() << endl; }
	~A(){ cout << "[A::A()析构函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl; }

	void thread_work(int num)
	{
		cout << "子线程 work 函数执行!] " << this << " threadid is " << std::this_thread::get_id() << endl;
	}
};

int main()
{
	A  myobj4(10);
	//thread mytobj4(&A::thread_work, myobj4, 15);// 可以用detach
	thread mytobj4(&A::thread_work, std::ref(myobj4), 15);//不能用detach,因为std::ref(myobj4)是引用。
	mytobj4.join();
	
	cout << "I QUIT! \n" << endl;

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宛如新生

转发即鼓励,打赏价更高!哈哈。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值