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

相关推荐
肆忆_11 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星15 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
郑州光合科技余经理4 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1234 天前
matlab画图工具
开发语言·matlab
dustcell.4 天前
haproxy七层代理
java·开发语言·前端
norlan_jame4 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc