c++ thread的使用 调用类里面的函数和调用类外的函数的区别

1.thread 调用类外的函数。

在使用thread之前要加上#include <thread>。

例1:

cpp 复制代码
#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	cout << "Threadfunc1" << endl;
}

void Threadfunc2(int vaule)
{
	cout << "Threadfunc1:" <<vaule<< endl;
}

int main(int argc, char* argv[])
{
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
    getchar();
	return 0;
}

如果有调用的线程函数参数,那么就像这样thread thread2(Threadfunc2,3);

在形参函数后面加上它的参数。

2.thread 调用类内的函数

示例代码:

cpp 复制代码
#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	cout << "Threadfunc1" << endl;
}

void Threadfunc2(int vaule)
{
	cout << "Threadfunc1:" <<vaule<< endl;
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc1:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	getchar();
	return 0;
}

//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去

//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。

//第三个参数是传递函数需要的参数。

3.thread的函数,join()和detach()的区别

join():调用了这个函数,主程序要等线程结束了自己才会结束

detach():主线程者创建的线程就分离了,主程序做完了自己的事情就会结束。创建的线程会做完了自己的事情才会结束。各干各的互不影响。

代码示例:

cpp 复制代码
#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	cout << "Threadfunc1" << endl;
	
}

void Threadfunc2(int vaule)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	cout << "Threadfunc2:" <<vaule<< endl;
	
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc3:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	thread1.join();
	thread2.join();
	thread3.join();

	//thread1.detach();
	//thread2.detach();
	//thread3.detach();

	return 0;
}

这段程序,主程序会卡在那里,等待所有的线程执行完成。

示例2:

cpp 复制代码
#include <iostream>
#include <thread>
using namespace std;
void Threadfunc1()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	cout << "Threadfunc1" << endl;
	
}

void Threadfunc2(int vaule)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	cout << "Threadfunc2:" <<vaule<< endl;
	
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc3:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	//thread1.join();
	//thread2.join();
	//thread3.join();

	thread1.detach();
	thread2.detach();
	thread3.detach();

	return 0;
}

这段程序,主程序会直接结束。但是其他的线程还是自己还执行的。

通过打印可以看到。主程序的确是自己先结束了的。主程序和创建的线程确实是分离了,不会相互阻塞

还有一个点要注意,主进程,主程序结束了。那里面的各种线程也会自己结束的。

示例代码

cpp 复制代码
#include <iostream>
#include <thread>
#include <Windows.h>
using namespace std;
void Threadfunc1()
{
	std::this_thread::sleep_for(std::chrono::milliseconds(5000));
	cout << "Threadfunc1" << endl;
	
}

void Threadfunc2(int vaule)
{
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	cout << "Threadfunc2:" <<vaule<< endl;
	
}

class Task
{
public:
	void Taskfunc(int value)
	{
		cout << "Threadfunc3:" << value << endl;
	}
};

int main(int argc, char* argv[])
{
	Task task_object;
	thread thread1(Threadfunc1);
	thread thread2(Threadfunc2,3);
	//如果调用的函数是在类里面,那么首先要在函数前面加上对应的类名,还要用取地址符,获取函数的地址传递过去
	//第二个参数是类的实例化对象,如果这段代码是在类里面的化那么这个参数就是用this来代替。
	//第三个参数是传递函数需要的参数。
	thread thread3(&Task::Taskfunc, task_object, 3);
	//thread1.join();
	//thread2.join();
	//thread3.join();

	thread1.detach();
	thread2.detach();
	thread3.detach();
	getchar();
	return 0;
}

对应输出

通过增加getchar(); 函数让主程序一直不会结束,那里面的子线程,也会一直执行。

但是使用了detach() ,让创建的线程和主程序相关独立

相关推荐
沐怡旸6 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4169 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥12 小时前
C++ std::set
c++
侃侃_天下12 小时前
最终的信号类
开发语言·c++·算法
博笙困了13 小时前
AcWing学习——差分
c++·算法
echoarts13 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix13 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
青草地溪水旁13 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁13 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥13 小时前
C++ std::vector
c++