C++ multithreading learning (2. Several ways of creating multithreading [we will talk about the return value later])

Table of contents

Create multiple threads

1. Ordinary functions act as thread processing functions to create threads

2. Lambda expressions act as thread processing functions

3. Create a thread with a parameter function

3.1 Common parameters

3.2 Incoming references

3.3 Smart pointers act as function parameters

4. Created by member functions in the class

4.1 Function-like creation: call by class name

4.2 Member functions in ordinary classes


Create multiple threads

1. Ordinary functions act as thread processing functions to create threads

#include <thread>
#include <iostream>
using namespace std;
void ThreadPrint()
{
	cout << "线程启动" << endl;
}
void ThreadOpen()
{
	thread t1(ThreadPrint);//无输入无返回值的函数
	t1.join();
}
int main()
{
	ThreadOpen();


	return 0;
}

2. Lambda expressions act as thread processing functions

#include <thread>
#include <iostream>
using namespace std;

void ThreadLambda()
{
	thread t1([]() {cout << "Lambda表达式充当线程处理函数" << endl; });
	t1.join();
}
int main()
{
	

	ThreadLambda();
	return 0;
}

3. Create a thread with a parameter function

3.1 Common parameters

#include <thread>
#include <iostream>
using namespace std;

void WhatYourYear(int num)//const int num 也是可以的
{
	cout << "your year:" << num << endl;
}
void ThreadHaveInt(int Pnum)//创建带参数的普通线程,你需要有这个数
{
	int num = 1;
	thread t1(WhatYourYear, num);
	t1.join();
	thread t2(WhatYourYear, Pnum);
	t2.join();
}
int main()
{
	ThreadHaveInt(9);

	
	return 0;
}

3.2 Incoming references

#include <thread>
#include <iostream>
using namespace std;

void IntoRef(int& num)//如果加上const,把 num=1001删除
{
	num = 1001;
	cout << "子线程" << num << endl;
}
void ThreadHaveRef()
{
	int num = 0;
	thread t1(IntoRef, ref(num));//这里thread后面传入的为&&,所以对num要包装引用作为传递的值
	t1.join();
	cout << "主线程" << num << endl;
}
int main()
{
	ThreadHaveRef();

	
	return 0;
}

3.3 Smart pointers act as function parameters

Because the smart pointer cannot be copied, it is a resource management object with ownership, so it cannot be directly passed to the function of the child thread.

Therefore, by using the move function to move the smart pointer ptr from the main thread to a new sub-thread, transfer the ownership of ptr to the sub-thread, so that the resources of the smart pointer can be correctly accessed in the sub-thread, and the smart pointer can be realized. Ownership is transferred from one context to another.

#include <thread>
#include <iostream>
using namespace std;

void printPtr(unique_ptr<int> ptr)
{
	cout << "智能指针" << ptr.get()<< endl;
}
void ThreadSmartPtr()
{
	unique_ptr<int> ptr(new int(100));
	cout << "主线程打印:" << ptr.get() << endl;
	thread t1(printPtr, move(ptr));//move语句作为处理
	t1.join();
	cout << "转移后的主线程打印:" << ptr.get() << endl;//是空的
}
int main()
{
	ThreadSmartPtr();

	
	return 0;
}

4. Created by member functions in the class

4.1 Function-like creation: call by class name

#include <thread>
#include <iostream>
using namespace std;

class TestFunction
{
public:
	void operator()()
	{
		cout << "重载()" << endl;
	}
};
void ThreadOperator()
{
	TestFunction TF1;//直接通过类创建对象,将对象传入线程
	thread t1(TF1);//这样就会调用一下void operator()()
	t1.join();
	//匿名对象
	thread t2((TestFunction()));//多加一个()帮助进行解析
	t2.join();
}
int main()
{
	ThreadOperator();

	
	return 0;
}


4.2 Member functions in ordinary classes

#include <thread>
#include <iostream>
using namespace std;

class LH
{
public:
	void print(int& num)//自己选择用什么模式的函数
	{
		cout << "people:" << num << endl;
	}
};
void ThreadClassOne()
{
	LH a;//指定对象
	int num = 1;
	thread t1(&LH::print, a, ref(num));//哪个类的函数,哪个对象,传入的值
	t1.join();
}
int main()
{
	
	ThreadClassOne();
	
	return 0;
}


Guess you like

Origin blog.csdn.net/q244645787/article/details/131525118