千家信息网

C++11中的多线程初探是怎样的

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,C++11中的多线程初探是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。相信 Linux 程序员都用过 Pthread, 但有
千家信息网最后更新 2026年11月01日C++11中的多线程初探是怎样的

C++11中的多线程初探是怎样的,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

相信 Linux 程序员都用过 Pthread, 但有了 C++11 的 std::thread 以后,你可以在语言层面编写多线程程序了,直接的好处就是多线程程序的可移植性得到了很大的提高,所以作为一名 C++ 程序员,熟悉 C++11 的多线程编程方式还是很有益处的。

如果你对 C++11 不太熟悉,我也收集了一些关于C++11的资料,供大家查阅:

资料汇http://www.open-std.org/jtc1/sc22/wg21/

C++0x/C++11 Support in GCC:http://gcc.gnu.org/projects/cxx0x.html

What is C++0x:https://www2.research.att.com/~bs/what-is-2009.pdf

Overview of the New C++:http://www.artima.com/shop/overview_of_the_new_cpp

Overview of the New C++ (C++0x).pdf:http://ishare.iask.sina.com.cn/f/20120005.html?from=likeA Brief Look at C++0x:http://www.artima.com/cppsource/cpp0x.html

Summary of C++11 Feature Availability in gcc and MSVC:http://www.aristeia.com/C++11/C++11FeatureAvailability.htm

C++ 11: Come Closer:http://www.codeproject.com/Articles/344282/Cplusplus-11-Come-Closer

C++11 threads, locks and condition variables: http://www.codeproject.com/Articles/598695/Cplusplus11-threads-locks-and-condition-variables

Move Semantics and Perfect Forwarding in C++11:http://www.codeproject.com/Articles/397492/Move-Semantics-and-Perfect-Forwarding-in-Cplusplus

http://solarianprogrammer.com/categories/C++11/

C++11 Concurrency:http://www.baptiste-wicht.com/2012/03/cpp11-concurrency-part1-start-threads/

http://www.hpl.hp.com/personal/Hans_Boehm/misc_slides/sfacm-cleaned.pdf

http://en.cppreference.com/w/cpp/thread

http://isocpp.org/blog/2012/12/c11-a-cheat-sheet-alex-sinyakov

The Biggest Changes in C++11:http://blog.smartbear.com/c-plus-plus/the-biggest-changes-in-c11-and-why-you-should-care/

Ten C++11 Features Every C++ Developer Should Use:http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer

C++11 - A Glance [part 1 of n]:http://www.codeproject.com/Articles/312029/Cplusplus11-A-Glance-part-1-of-n

C++11 - A Glance [part 2 of n]:http://www.codeproject.com/Articles/314415/Cplusplus11-A-Glance-part-2-of-n

C++11(及现代C++风格)和快速迭代式开发:http://mindhacks.cn/2012/08/27/modern-cpp-practices/

Lambda Functions in C++11 - the Definitive Guide:http://www.cprogramming.com/c++11/c++11-lambda-closures.html

Better types in C++11 - nullptr, enum classes (strongly typed enumerations) and cstdint:http://www.cprogramming.com/c++11/c++11-nullptr-strongly-typed-enum-class.html

Rvalue-references-and-move-semantics-in-c++11:http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html

http://www.gotw.ca/publications/index.htm

http://www.devx.com/SpecialReports/Door/38865

Multi-threading in C++0x:http://accu.org/index.php/journals/1584

C++ 0X feature summary cheat sheat:http://www.iesensor.com/blog/2011/05/31/c-0x-feature-summary-cheat-sheat/

Multithreading in C++0x part 1: Starting Threads:http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-1-starting-threads.html

http://en.cppreference.com/w/cpp/thread

http://www.cplusplus.com/reference/multithreading/

好了,下面来说正题吧

与 C++11 多线程相关的头文件

C++11 新标准中引入了四个头文件来支持多线程编程,他们分别是 ,,,和。

:该头文主要声明了两个类, std::atomic 和 std::atomic_flag,另外还声明了一套 C 风格的原子类型和与 C 兼容的原子操作的函数。 :该头文件主要声明了 std::thread 类,另外 std::this_thread 命名空间也在该头文件中。 :该头文件主要声明了与互斥量(mutex)相关的类,包括 std::mutex 系列类,std::lock_guard, std::unique_lock, 以及其他的类型和函数。 :该头文件主要声明了与条件变量相关的类,包括 std::condition_variable 和 std::condition_variable_any。 :该头文件主要声明了 std::promise, std::package_task 两个 Provider 类,以及 std::future 和 std::shared_future 两个 Future 类,另外还有一些与之相关的类型和函数,std::async() 函数就声明在此头文件中。

std::thread "Hello world"

下面是一个最简单的使用 std::thread 类的例子:

#include #include #include // std::cout#include // std::threadvoid thread_task() { std::cout << "hello thread" << std::endl;}/* * === FUNCTION ========================================================= * Name: main * Description: program entry routine. * ======================================================================== */int main(int argc, const char *argv[]){ std::thread t(thread_task); t.join(); return EXIT_SUCCESS;} /* ---------- end of function main ---------- */

Makefile 如下:

all:ThreadCC=g++CPPFLAGS=-Wall -std=c++11 -ggdbLDFLAGS=-pthreadThread:Thread.o $(CC) $(LDFLAGS) -o $@ $^Thread.o:Thread.cc $(CC) $(CPPFLAGS) -o $@ -c $^.PHONY: cleanclean: rm Thread.o Thread

注意在 Linux GCC4.6 环境下,编译时需要加 -pthread,否则执行时会出现:

$ ./Threadterminate called after throwing an instance of 'std::system_error' what(): Operation not permittedAborted (core dumped)

原因是 GCC 默认没有加载 pthread 库,据说在后续的版本中可以不用在编译时添加 -pthread 选项。

关于C++11中的多线程初探是怎样的问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

文件 线程 C++ 函数 程序 两个 类型 问题 原子 更多 程序员 资料 风格 帮助 编程 编译 解答 很大 易行 简单易行 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海喔噻互联网科技有限公司 重庆互联网络科技 互联网金融 金融科技 狮子座互联网科技 广州善班互联网科技 火传媒上海互联网科技 滨湖区互联网智能科技产品供应 济南和联互联网科技有限公司 银川新华互联网科技学校地址 博兴的互联网电子科技公司 河南猎农互联网科技有限公司 米聚互联网科技南京有限公司排面 深圳互联网科技的发展 江苏苏星互联网科技有限公司 21世纪互联网科技成就 容联科技互联网电商 互联网科技机房 互联网界的黑科技 互联网公司属于科技吗 保山浩联互联网科技有限公司 互联网扶贫科技 互联网高科技企业 互联网金融监管科技 佩信高科技互联网 中国航天科技大学国际工业互联网 世界互联网领先科技成果获奖 山东有哪些互联网科技公司 国大互联网教育科技有限公司薪资 张家口科技产业互联网平台 互联网科技的前景
0