C++11_14_17_20多线程

2023-12-20 07:09:05

@TOC

为什么使用多线程?

任务分解

耗时的操作。
任务分解。
实时响应。

数据分解

利用多核CPU处理数据。(base64编码,音视频或图像的处理。)

数据流分解

读写分离,解耦合设计。(微服务模块多线程通信。)

线程创建的方式

全局函数作为线程入口函数

参数传递

线程参数传递对象的过程:

#include <thread>
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass() { cout << "MyClass()" << endl; }
	MyClass(const MyClass& another) { cout << "copy MyClass" << endl; }
	~MyClass() { cout << "~MyClass" << endl; }

private:
};

void TthreadFunction(int i,float f,string s, MyClass mc)
{
	cout<< "begin TthreadFunction()" << endl;
	cout << "i:" << i << " f:" << f << " s:" << s << endl;
	cout << "end TthreadFunction()" << endl;
}

int main()
{
	cout << "begin main()" <<endl;
	MyClass mc;
	thread t1(TthreadFunction,1,3.14,"test", mc);
	cout << "wait main()" << endl;
	t1.join();
	cout << "end main()" << endl;
	return 0;
}

运行结果:
运行结果
通过传递指针或引用避免对象拷贝:

#include <thread>
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass() { cout << "MyClass()" << endl; }
	MyClass(const MyClass& another) { cout << "copy MyClass" << endl; }
	~MyClass() { cout << "~MyClass" << endl; }

	string test;
private:
};

void TthreadFunctionPtr(MyClass * mc)
{
	cout << "begin TthreadFunctionPtr()" << endl;
	cout << "mc.test: " << mc->test << endl;
	cout << "end TthreadFunctionPtr()" << endl;
}

int main()
{
	cout << "begin main()" <<endl;
	MyClass mc;
	mc.test = "data";
	thread t1(TthreadFunctionPtr, &mc);
	cout << "wait main()" << endl;
	t1.join();
	cout << "end main()" << endl;
	return 0;
}

运行结果:
运行结果

#include <thread>
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass() { cout << "MyClass()" << endl; }
	MyClass(const MyClass& another) { cout << "copy MyClass" << endl; }
	~MyClass() { cout << "~MyClass" << endl; }

	string test;
private:
};

void TthreadFunctionRef(MyClass& mc)
{
	cout << "begin TthreadFunctionPtr()" << endl;
	cout << "mc.test: " << mc.test << endl;
	cout << "end TthreadFunctionPtr()" << endl;
}

int main()
{
	cout << "begin main()" <<endl;
	MyClass mc;
	mc.test = "data";
	thread t1(TthreadFunctionRef, ref(mc));
	cout << "wait main()" << endl;
	t1.join();
	cout << "end main()" << endl;

	return 0;
}

运行结果:
运行结果

传递对象指针或引用生命周期 < 线程生命周期:
static。动态内存。指针对象作为类的数据成员,确保线程生命周期和对象生命周期相同。

成员函数作为线程入口

#include <thread>
#include <iostream>

using namespace std;

class MyClass
{
public:
	MyClass() { cout << "MyClass()" << endl; }
	MyClass(const MyClass& another) { cout << "copy MyClass" << endl; }
	~MyClass() { cout << "~MyClass" << endl; }

	void Display()
	{
		cout << "test: " << test << endl;
	}

	string test;
private:
};

int main()
{
	cout << "begin main()" <<endl;
	MyClass mc;
	mc.test = "data";
	thread t1(&MyClass::Display, &mc);
	cout << "wait main()" << endl;
	t1.join();
	cout << "end main()" << endl;

	return 0;
}

运行结果:
运行结果

封装一个线程基类

业务需要创建线程时继承基类,线程的启动,停止等都在基类中完成。
只维护一个线程对象。
隐藏线程内部结构。

实现Main函数,线程就创建了。

#include <thread>
#include <iostream>

using namespace std;


class XThread
{
public:
	virtual void Start()
	{
		is_exit_ = false;
		th_ = std::thread(&XThread::Main, this);
	}

	virtual void Stop()
	{
		is_exit_ = true;
		Wait();
	}

	virtual void Wait()
	{
		if (th_.joinable())
		{
			th_.join();
		}
	}

	bool is_exit()
	{
		return is_exit_;
	}

private:
	bool is_exit_ = false;
	std::thread th_;
	virtual void Main() = 0;	//线程启动的入口函数
};


class XThreadTest:public XThread
{
public:
	void Main() override
	{
		while (!is_exit())
		{
			cout << "." << endl;
			this_thread::sleep_for(1s);
		}
	}

	string test;

};


int main()
{
	XThreadTest test;	//只维护这一个对象的生命周期
	test.test = "test";
	test.Start();
	this_thread::sleep_for(5s);
	test.Stop();
	return 0;
}

运行结果:
运行结果

文章来源:https://blog.csdn.net/qq_43648751/article/details/135095217
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。