news 2026/6/10 11:31:15

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
C++11线程库的使用(上)

1.线程库的基本使用

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

#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结束啦,就导致出现了错误。那么怎么办呢?我们来看下面这段程序:

#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()就解决了这个问题,这是因为当这个调用时一个阻塞调用,主线程运行到这里后就会被阻塞,只有当子线程结束后主线程才能进行往下执行。所以就解决了上面子线程还没有结束主线程就结束了导致的报错,我们将上面的程序修改一下会有更明显得体现,如下:

#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的输出是在子线程输出完后再输出的。当然我们还可以是用另一个方法来解决上面报错问题。

#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时子线程和主线程分离了,子线程进入后台执行,而主线程后面就立即结束了,所以就没有输出,但是主线程结束后子线程还可以自动在后台运行。接着我们来看看当要传入参数怎么书写程序:

#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)线程间传递临时变量

我们来看下面的程序:

#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来解决该问题程序如下所示:

#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)传递指针和引用时指向局部变量

我们看下面这段程序:

#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)类成员函数作为入口参数,类对象被提前释放

我们看下面这段程序:

#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被提前销毁了,而线程想要使用类的成员函数就会出现问题。对于这个问题我们可以使用智能指针的方法来解决。看下面的程序:

#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)使用类的私有成员函数作为线程的入口参数

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

#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; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/9 3:40:30

Arturo:在“单词”与“符号”之间,追寻一种“纯正”的语法

思维导图在这&#xff1a;https://www.anygraphanywhere.com/automap/mindmap.html?link1765710459448-1765710459448 在编程语言设计的万神殿中&#xff0c;主流思潮往往围绕两条轴线展开&#xff1a;一条是追求极致的性能与控制&#xff0c;如C与Rust&#xff1b;另一条是构…

作者头像 李华
网站建设 2026/6/9 16:19:31

ContextMenuManager:快速解决Windows右键菜单臃肿问题的终极方案

ContextMenuManager&#xff1a;快速解决Windows右键菜单臃肿问题的终极方案 【免费下载链接】ContextMenuManager &#x1f5b1;️ 纯粹的Windows右键菜单管理程序 项目地址: https://gitcode.com/gh_mirrors/co/ContextMenuManager 你是否曾为Windows右键菜单中堆积如…

作者头像 李华
网站建设 2026/6/8 8:09:18

Quill图片调整模块:5分钟快速上手终极指南

Quill图片调整模块&#xff1a;5分钟快速上手终极指南 【免费下载链接】quill-image-resize-module A module for Quill rich text editor to allow images to be resized. 项目地址: https://gitcode.com/gh_mirrors/qui/quill-image-resize-module Quill图片调整模块是…

作者头像 李华
网站建设 2026/6/5 10:34:16

SuperCom串口调试工具深度使用指南

SuperCom串口调试工具深度使用指南 【免费下载链接】SuperCom SuperCom 是一款串口调试工具 项目地址: https://gitcode.com/gh_mirrors/su/SuperCom 在嵌入式开发和硬件调试领域&#xff0c;串口通信是最基础也是最重要的调试手段之一。SuperCom作为一款功能全面的串口…

作者头像 李华
网站建设 2026/6/9 23:33:09

macOS平台百度网盘下载性能优化技术深度解析

1. 系统性能瓶颈分析 【免费下载链接】BaiduNetdiskPlugin-macOS For macOS.百度网盘 破解SVIP、下载速度限制~ 项目地址: https://gitcode.com/gh_mirrors/ba/BaiduNetdiskPlugin-macOS 百度网盘作为国内主流云存储服务&#xff0c;在macOS平台存在显著的下载性能限制问…

作者头像 李华
网站建设 2026/6/8 4:04:46

TouchGAL技术架构深度解析:构建高性能Galgame社区平台

TouchGAL技术架构深度解析&#xff1a;构建高性能Galgame社区平台 【免费下载链接】kun-touchgal-next TouchGAL是立足于分享快乐的一站式Galgame文化社区, 为Gal爱好者提供一片净土! 项目地址: https://gitcode.com/gh_mirrors/ku/kun-touchgal-next TouchGAL是一个基于…

作者头像 李华