C++11线程库的使用(上)

1.线程库的基本使用

在C++11中引入了原生的线程支持,包含在<thread>头文件中,我们下面来看看该线程库的基本使用方法,先看下面这段程序。

cpp 复制代码
#include <iostream>
#include <thread>

void show(void)
{
	std::cout << "hello thread" << std::endl;
}

int main()
{
	std::thread thread(show);
	std::cout << "main" << std::endl;
	return 0;
}

我们创建了一个线程对象,将show函数作为入口传入。当我们运行时有以下运行结果:

虽然我们的hello thread成功输出出来了,但是却出现了错误,这是因为在我们子线程还没有结束时,我们的main线程已经return结束啦,就导致出现了错误。那么怎么办呢?我们来看下面这段程序:

cpp 复制代码
#include <iostream>
#include <thread>

void show(void)
{
	std::cout << "hello thread" << std::endl;
}

int main()
{
	std::thread thread(show);
	std::cout << "main" << std::endl;
	thread.join();
	return 0;
}

我们再来看运行结果:

可见我们在main结束前加入了一句thread.join()就解决了这个问题,这是因为当这个调用时一个阻塞调用,主线程运行到这里后就会被阻塞,只有当子线程结束后主线程才能进行往下执行。所以就解决了上面子线程还没有结束主线程就结束了导致的报错,我们将上面的程序修改一下会有更明显得体现,如下:

cpp 复制代码
#include <iostream>
#include <thread>

void show(void)
{
	for (int i = 0; i < 100000; i++)
	{
		std::cout << "hello thread" << std::endl;
	}
}

int main()
{
	std::thread thread(show);
	thread.join();
	std::cout << "main" << std::endl;
	return 0;
}

运行结果为:

可见mian的输出是在子线程输出完后再输出的。当然我们还可以是用另一个方法来解决上面报错问题。

cpp 复制代码
#include <iostream>
#include <thread>

void show(void)
{
	std::cout << "hello thread" << std::endl;
}

int main()
{
	std::thread thread(show);
	std::cout << "main" << std::endl;
	thread.detach();
	return 0;
}

运行结果为:

我们可以看到确实没有报错了,但是我们的hellothread确没有被输出出来。这是因为当主函数运行到thread.detach时子线程和主线程分离了,子线程进入后台执行,而主线程后面就立即结束了,所以就没有输出,但是主线程结束后子线程还可以自动在后台运行。接着我们来看看当要传入参数怎么书写程序:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
using namespace std;

void show(string s)
{
	cout << s << endl;
}

int main()
{
	thread thread(show,"hello thread");
	cout << "main" << endl;
	thread.join();
	return 0;
}

我们只需要在创建thread对象,传入一个所需的参数即可。看运行结果:

2.线程使用过程中的易错问题

(1)线程间传递临时变量

我们来看下面的程序:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
using namespace std;

void process(int& n)
{
	n += 1;
}

int main()
{
	int data = 1;
	thread thread(process,data);
	thread.join();
	cout << "data=" << data << endl;
	return 0;
}

我们想启用一个线程process对我们创建的data进行加1操作时用上面的程序能不能做到呢,看运行结果:

可见直接出现了错误,这是因为我们在线程间传递临时变量出现的错误,我们通过std::ref来解决该问题程序如下所示:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
using namespace std;

void process(int& n)
{
	n += 1;
}

int main()
{
	int data = 1;
	thread thread(process,ref(data));
	thread.join();
	cout << "data=" << data << endl;
	return 0;
}

(2)传递指针和引用时指向局部变量

我们看下面这段程序:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
using namespace std;

thread t;
void process(int& n)
{
	n += 1;
}

void func(void)
{
	int data = 1;
	t =  thread(process, ref(data));
}

int main()
{
	func();
	t.join();
	return 0;
}

我们可以知道data局部变量在func函数结束后会被释放,所以等到引用n在使用data那块空间时已经被释放了所以就会出现报错,指针也是一样的,那么怎么解决呢?我们可以将data定义为全局变量即可。

(3)类成员函数作为入口参数,类对象被提前释放

我们看下面这段程序:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
using namespace std;


class A {
public:
	void show()
	{
		cout << "A" << endl;
	}
};


int main()
{
	A a;
	thread t(&A::show,&a);
	return 0;
}

当线程的入口参数时类成员函数时,我们还要将类的对象的地址也作为参数传入。这里因为我们创建一个对象所以我们要进行销毁,但是销毁的时机我们却不好把握。这里如果当对象a被提前销毁了,而线程想要使用类的成员函数就会出现问题。对于这个问题我们可以使用智能指针的方法来解决。看下面的程序:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
#include <memory>
using namespace std;

thread t;
class A {
public:
	void show()
	{
		cout << "A" << endl;
	}
};



int main()
{
	shared_ptr<A> a = make_shared<A>();
	thread t(&A::show, a);
	t.join();
	return 0;
}

对于智能指针来说,当其在没有地方使用时就会自动释放掉,既不会造成内存的泄露,也能更好的让线程执行类的成员函数。

(4)使用类的私有成员函数作为线程的入口参数

对于这个问题,我们可以使用友元函数进行解决,如下:

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
#include <memory>
using namespace std;

thread t;
class A {
private:
	friend void func();
	void show()
	{
		cout << "A" << endl;
	}
};


void func()
{
	shared_ptr<A> a = make_shared<A>();
	thread t(&A::show, a);
	t.join();
}


int main()
{
	func();
	return 0;
}
相关推荐
睡美人的小仙女12712 小时前
Threejs加载环境贴图报错Bad File Format: bad initial token
开发语言·javascript·redis
rayufo12 小时前
【工具】列出指定文件夹下所有的目录和文件
开发语言·前端·python
RANCE_atttackkk12 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
数研小生13 小时前
构建命令行单词记忆工具:JSON 词库与艾宾浩斯复习算法的完美结合
算法·json
芒克芒克13 小时前
LeetCode 题解:除自身以外数组的乘积
算法·leetcode
缺点内向13 小时前
C#编程实战:如何为Word文档添加背景色或背景图片
开发语言·c#·自动化·word·.net
一起养小猫13 小时前
Flutter for OpenHarmony 实战:记账应用数据统计与可视化
开发语言·jvm·数据库·flutter·信息可视化·harmonyos
zhougl99613 小时前
Java 所有关键字及规范分类
java·开发语言
Python 老手13 小时前
Python while 循环 极简核心讲解
java·python·算法
@Aurora.13 小时前
优选算法【专题九:哈希表】
算法·哈希算法·散列表