C++11 多线程编程 使用lambda创建std::thread (生产/消费者模式)
要写个tcp server / client的博客,想着先写个c++11多线程程序。方便后面写博客使用。
目前c++11中写多线程已经很方便了,不用再像之前的pthread_create,c++11中已经有了std::thread库可以方便使用。
直接看代码(100个任务, 多个线程处理):
#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
#include <mutex> class Task{
public:
Task(int x, std::shared_ptr<std::mutex> mutex)
:_x(x), _mutex(mutex){ } void handle(){
int task_id = ;
while(true){
//获取任务, 尽早释放锁
if (_x > ){
std::lock_guard<std::mutex> lock(*_mutex);
if (_x > ){ task_id = _x; --_x; }
else { _x = ; }
}
else { return ; } //do task
std::cout << "do task id: " << task_id << std::endl;
std::this_thread::sleep_for(std::chrono::seconds());
}
}
private:
int _x;
std::shared_ptr<std::mutex> _mutex;
}; int main()
{
int x = ;
const int THREAD_NUM = ;
const int TASK_NUM = ;
std::vector<std::thread> threads; //shared_ptr 主线程与子线程可以共用一把锁.
//方便后面扩展程序(生产/消费者)
std::shared_ptr<std::mutex> mutex = std::make_shared<std::mutex>();
Task t(TASK_NUM, mutex); //新建线程, std::thread支持使用lambda
for (int i = ; i < THREAD_NUM; ++i){
threads.emplace_back(std::thread(
[&t] { t.handle(); })
);
} //等待线程结束
for(auto &thread : threads){ thread.join(); }
return ;
}
编译、执行:
g++ --std=c++11 -pthread thread.cpp
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQoAAACyCAIAAAA1RxYyAAAAA3NCSVQICAjb4U/gAAAbfElEQVR4Xu2deVxN+f/Hz7237VYqLUIJI/vaz5Yy1mzNjOVrMiGS0aRULkKTIskUpkaYyVIYJKUy1iYGGaSi0qI9LW6KaLstbsu9v3OXaDn3dpvOqdvtfR7+cT7nvD+f9/P9eZ+tz+u+SZPkdRHYgAAQwCJAxtoJ+4AAEOAQgPSAeQAEBBLASg/ZId+47rUyUsdqE2iI2AaK2tdHLpw7MUOBoG7E0GWCPAWzHSGAlQKyust3rjMaI4/V1gHbJIVB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tBCA9emvkwW8RCGClB6gFRQAHh/QGApjpAWrB3hB68LF9Aljp0f5ZIh0BasE2mEjy4zeeepeVkGQ/DP2TKGxiTwDUgriFSJhakNuJzEgzrwiHkbh1CIYIJ8C/e4BakFi1IKoXVJ25M8xB+Y9N+25VEB5V6AAnAqAW7Aq1ICKlucjXe37intWeCVq+OIUOzBBPANSCXaAWpPRf7fHzlEd7pt951yCrRXxQoQe8CHAerkAtiEUTN7Ugud98B4+J8bsOP4VSm1igxXkf990D1IIEhoikPMdifl+l2Rei4qrRn7pK8l+jjAyzC3kfsnIQv9gggZ2D6c4R4KQHqAWxGOKlFmRXhDusmLxkJf/f//beqUEKL28z3Hq3CH7LBwu8OO0DtSDxakFGYV7655hTVcrrkU8f32QVMqD0pjhlAuZYQC3YRiDJ44SjWhATPOzsCQRALdgTogRj7CYC/w/rSw/MeUSetgAAAABJRU5ErkJggg==" alt="" />
C++11 多线程编程 使用lambda创建std::thread (生产/消费者模式)的更多相关文章
- day11(多线程,唤醒机制,生产消费者模式,多线程的生命周期)
A:进程: 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. B:线程: 线程是进程中的一个执行单元,负责当前进程中程序的执 ...
- C++11多线程编程--线程创建
参考资料 adam1q84 我是一只C++小小鸟 Thread support library Book:<C++ Concurrency in Action> 线程的创建 线程的创建有多 ...
- C++11多线程编程
1. 多线程编程 在进行桌面应用程序开发的时候, 假设应用程序在某些情况下需要处理比较复杂的逻辑, 如果只有一个线程去处理,就会导致窗口卡顿,无法处理用户的相关操作.这种情况下就需要使用多线程,其中一 ...
- C++ 11 笔记 (五) : std::thread
这真是一个巨大的话题.我猜记录完善绝B需要一本书的容量. 所以..我只是略有了解,等以后用的深入了再慢慢补充吧. C++写多线程真是一个痛苦的事情,当初用过C语言的CreateThread,见过boo ...
- .net学习之多线程、线程死锁、线程通信 生产者消费者模式、委托的简单使用、GDI(图形设计接口)常用的方法
1.多线程简单使用(1)进程是不执行代码的,执行代码的是线程,一个进程默认有一个线程(2)线程默认情况下都是前台线程,要所有的前台线程退出以后程序才会退出,进程里默认的线程我们叫做主线程或者叫做UI线 ...
- Java多线程学习笔记--生产消费者模式
实际开发中,我们经常会接触到生产消费者模型,如:Android的Looper相应handler处理UI操作,Socket通信的响应过程.数据缓冲区在文件读写应用等.强大的模型框架,鉴于本人水平有限目前 ...
- Linux多线程编程——线程的创建与退出
POSIX线程标准:该标准定义了创建和操纵线程的一整套API.在类Unix操作系统(Unix.Linux.Mac OS X等)中,都使用Pthreads作为操作系统的线程.Windows操作系统也有其 ...
- UNIX环境编程学习笔记(26)——多线程编程(一):创建和终止线程
lienhua342014-11-08 在进程控制三部曲中我们学习了进程的创建.终止以及获取终止状态等的进程控制原语.线程的控制与进程的控制有相似之处,在表 1中我们列出了进程和线程相对应的控制原语. ...
- C++11多线程编程(常见面试题)
[题目1] 子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码 [题解] 首先我们来分析一下这道题...( ...
随机推荐
- Idea xml 粘贴文本保持原有格式
setting->Editor->Code Style->XML 在右边的面板中,单击第二个 “Other” 的页签,勾选“Keep white spaces”,重启idea.
- css 文件连接不到网页
css 文件连接不到网页 编码错误,将编码改为utf-8 Rom后正常
- django----对model查询扩展
基于对象关联查询 一对多查询(Book--Publish): 正向查询,按字段: (从关联的表中查询) book_obj.publish : 与这本书关联的出版社对象 book_obj.publish ...
- python 全栈开发,Day106(结算中心(详细),立即支付)
昨日内容回顾 1. 为什么要开发路飞学城? 提供在线教育的学成率: 特色: 学,看视频,单独录制增加趣味性. 练,练习题 改,改学生代码 管,管理 测,阶段考核 线下:8次留级考试 2. 组织架构 - ...
- Linux权限命令
Linux 基础——权限管理命令chmod 一.Linux中的文件权限与目录权限 Linux中定义了3种访问权限,分别是r.w.x.其中r表示对象是可读的,w表示对象是可写的,x表示对象是可执行的 ...
- 《剑指offer》-旋转数组的最小数字
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数 ...
- 【AtCoder】KEYENCE Programming Contest 2019
A - Beginning 这个年份恐怕需要+2 #include <bits/stdc++.h> #define fi first #define se second #define p ...
- Mysql linux -N命令
一.-e 和 -N /usr/local/bin/mysql -h127.0.0.1 -uroot -pxxxx -N -e "use abc; show tables;" -e ...
- Python 可命名元祖
import collections MytupleClass = collections.namedtuple('MytupleClass',['x','y','z']) obj = Mytuple ...
- Frosh Week HDU3743(逆序数)
离散化加 求逆序数: 求逆序数的方法 一个是归并排序 一个是树状数组 #include<bits/stdc++.h> using namespace std; int n; struct ...