最近使用C++11std::thread比较多,这里开始好好地总结一下。

官方的参考文档:https://en.cppreference.com/w/cpp/thread/thread
大佬的博客:https://www.cnblogs.com/haippy/p/3235560.html

1 多线程的创建和使用
2 线程的joindetach
3 互斥锁mutex的使用

多线程的创建和使用

在C语言中,我们比较常用的多线程是pthread(POSIX Threads),在类Unix的系统中,使用比较广泛,当然在Windows系统下也有第三方的libpthread-win的库可以使用,不过C++11中提供了std::thread
的库,这样可以在语言层面实现对系统层API的封装。也更加利于跨平台代码的编写。

需要包含头文件<thread>

下面是最简单的一个线程实例;

#include <iostream>
#include <thread>
using namespace std;void thread_func_1(int v){for (int i = 0; i < 5; i++) {std::cout << v << " thread_func_1 i:" << i << endl;}
}void thread_func_2(int v){for (int i = 0; i < 5; i++) {std::cout << v << " thread_func_2 i:" << i << endl;}
}int main()
{thread t1(thread_func_1, 1);thread t2(thread_func_2, 3);t1.join();t2.join();return 0;
}

我的运行平台是Ubuntu1804,这里直接使用命令行进行编译

g++ main.cpp -lpthread -o  run

原因是 GCC 默认没有加载 pthread 库,-pthread 选项
运行一下./run

最终输出结果:

资料汇总

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=like
A 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++ 多线程的创建和使用相关推荐

  1. Win32 多线程的创建方法,区别和联系

    2019独角兽企业重金招聘Python工程师标准>>> Win32多线程的创建方法主要有: CreateThread() _beginthread()&&_begin ...

  2. java线程的创建与执行_Java多线程的创建和运行

    1.多线程的好处 多线程是一个很有用的东西,它使的系统可以同时运行多个任务,提高程序的执行效率.大家平时可能没有注意到,其实我们电脑能同时执行多个程序的基本原理就是多线程. 每一个程序都是一个进程,而 ...

  3. c++:多线程的创建和unique_lock<mutex>的使用

    1.多线程的创建 1)包括头文件 #include<thread> #include<mutex> 2)新建线程 新建线程两个 使用thread 线程名来新建一个线程 并在线程 ...

  4. MFC多线程的创建,包括工作线程和用户界面线程

    MFC多线程的创建 1.MFC多线程简介 MFC对多线程进行了一层简单的封装,在Visual C++中每个线程都是从CWinThread类继承而来的.每一个应用程序的执行都有一个主线程,这个主线程也是 ...

  5. java中多线程的创建方式一:

    //多线程的创建: //方式一:继承于Thread类的方式进行创建 //1.创建一个继承Thread类的子类 //2.重写Thread类中的run()方法–>将此线程执行的操作写在run方法中 ...

  6. linux下多线程的创建与等待详解 【转载】

    linux下多线程的创建与等待详解 http://blog.chinaunix.net/uid-23842323-id-2656572.html 所有线程都有一个线程号,也就是Thread ID.其类 ...

  7. 一篇文章认识4种Java多线程的创建方式

    Java4种多线程的创建: 什么是程序? 什么是进程? 什么是线程? 并行与并发: 那么JAVA多线程实现方式: (1)继承Thread类实现多线程: (2)实现Runnable接口方式实现多线程: ...

  8. Java 并发 多线程:创建线程的四种方式

    Java 并发 多线程: 创建线程的四种方式 继承 Thread 类并重写 run 方法 实现 Runnable 接口 实现 Callable 接口 使用线程池的方式创建 1. 通过继承 Thread ...

  9. java多线程w3c_Java创建多线程的三种方式

    前言 这篇文章主要讲述线程的概念.组成.Java创建多线程的三种方式以及线程的类型. 线程概念 线程和进程的区别 **进程:**正在运行的程序,例如:你打开 的qq音乐.exe程序,其由PCB(进程控 ...

最新文章

  1. python的面向对象编程学生成绩_python的类_面向对象编程
  2. java只会用不知道原理6_程序员面试宝典之14道初级Java面试题分享
  3. mysqldump表损坏问题
  4. 【HTML/CSS】CSS权重、继承及引入方式
  5. 花音机器人_【扑杀花音攻略组】超弩级光机器人攻略
  6. django objects.filter().exists()
  7. Java基础知识强化54:经典排序之插入排序(InsertSort)
  8. Android之NDK开发(转)
  9. 如何从小白进化成 Apache Flink 技术专家?9节基础课程免费公开!
  10. Farseer Physics Engine
  11. 阅读 深入理解JVM虚拟机笔记一
  12. 关于极限精简版系统(RAMOS专用)的说明(FAQ)
  13. 爱加密脱壳(持续更新)
  14. 浏览器UserAgent的趣味史
  15. uint8_t范围_uint8_t / uint16_t / uint32_t /uint64_t数据类型详解
  16. iPhone/苹果手机不用数据线传输文件到电脑的方法/步骤
  17. React antd antd-mobile修改Switch组件尺寸大小
  18. python关键词挖掘_Python挖词脚本,挖出几十万关键词不是梦 带搜索量
  19. web前端需要学习什么?
  20. 冯·诺依曼体系结构的学习总结

热门文章

  1. amd为什么还用针脚_闲聊CPU针脚 一年一换都怪AMD不给力?
  2. MySQL精简版安装教程
  3. 使用linux集体升级系统,1.3. 利用mtd工具升级Linux系统
  4. Delphi开发OPC
  5. 桌面widget详解(四)——桌面音乐播放器(实战)
  6. json文件的读与写
  7. 校园网同时连手机和电脑、用数据线给电脑连网
  8. java生成二维码技术实现
  9. 免费数据库应用程序工具 - 不需代码
  10. 学python为何不好找工作呢?