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() ,让创建的线程和主程序相关独立

相关推荐
newki3 分钟前
学习笔记,关于NDK/JNI的简介与实战
android·c++·app
键盘林43 分钟前
分布式系统简述
java·开发语言
可儿·四系桜43 分钟前
如何在 Java 中优雅地使用 Redisson 实现分布式锁
java·开发语言·分布式
四谷夕雨1 小时前
C++八股 —— 单例模式
c++
Stanford_11061 小时前
关于大数据的基础知识(二)——国内大数据产业链分布结构
大数据·开发语言·物联网·微信小程序·微信公众平台·twitter·微信开放平台
一个何包蛋!!1 小时前
相关类相关的可视化图像总结
开发语言·python·数据可视化
超级土豆粉1 小时前
JavaScript 标签加载
开发语言·javascript·ecmascript
love530love1 小时前
教程:PyCharm 中搭建多级隔离的 Poetry 环境(从 Anaconda 到项目专属.venv)
开发语言·ide·人工智能·windows·python·pycharm
委婉待续2 小时前
Qt的学习(二)
c++·qt
重生之后端学习2 小时前
苍穹外卖-day03
java·开发语言·数据库·spring boot·mysql·spring·tomcat