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

相关推荐
代码雕刻家2 分钟前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
一个闪现必杀技5 分钟前
Python入门--函数
开发语言·python·青少年编程·pycharm
Fan_web9 分钟前
jQuery——事件委托
开发语言·前端·javascript·css·jquery
龙图:会赢的12 分钟前
[C语言]--编译和链接
c语言·开发语言
rjszcb31 分钟前
一文说完c++全部基础知识,IO流(二)
c++
小字节,大梦想1 小时前
【C++】二叉搜索树
数据结构·c++
吾名招财1 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn
XKSYA(小巢校长)2 小时前
NatGo我的世界联机篇
开发语言·php
Cons.W2 小时前
Codeforces Round 975 (Div. 1) C. Tree Pruning
c语言·开发语言·剪枝
我是哈哈hh2 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝