蓝桥ROS机器人之现代C++学习笔记7.4 条件变量
学习如下代码:
// condition_variable example
#include <iostream> // std::cout
#include <thread> // std::thread
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variablestd::mutex mtx;
std::condition_variable cv;
bool ready = false;void print_id (int id) {std::unique_lock<std::mutex> lck(mtx);while (!ready) cv.wait(lck);// ...std::cout << "thread " << id << '\n';
}void go() {std::unique_lock<std::mutex> lck(mtx);ready = true;cv.notify_all();
}int main ()
{std::thread threads[10];// spawn 10 threads:for (int i=0; i<10; ++i)threads[i] = std::thread(print_id,i);std::cout << "10 threads ready to race...\n";go(); // go!for (auto& th : threads) th.join();return 0;
}
#include <queue>
#include <chrono>
#include <mutex>
#include <thread>
#include <iostream>
#include <condition_variable>int main() {std::queue<int> produced_nums;std::mutex mtx;std::condition_variable cv;bool notified = false; // notification signauto producer = [&]() {for (int i = 0; ; i++) {std::this_thread::sleep_for(std::chrono::milliseconds(500));std::unique_lock<std::mutex> lock(mtx);std::cout << "producing " << i << std::endl;produced_nums.push(i);notified = true;cv.notify_all();}};auto consumer = [&]() {while (true) {std::unique_lock<std::mutex> lock(mtx);while (!notified) { // avoid spurious wakeupcv.wait(lock);}// temporal unlock to allow producer produces more rather than// let consumer hold the lock until its consumed.lock.unlock();std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // consumer is slowerlock.lock();if (!produced_nums.empty()) {std::cout << "consuming " << produced_nums.front() << std::endl;produced_nums.pop();}notified = false;}};std::thread p(producer);std::thread cs[2];for (int i = 0; i < 2; ++i) {cs[i] = std::thread(consumer);}p.join();for (int i = 0; i < 2; ++i) {cs[i].join();}return 0;
}
蓝桥ROS机器人之现代C++学习笔记7.4 条件变量相关推荐
- 蓝桥ROS机器人之现代C++学习笔记之路径规划
蓝桥云课有C和C++路径,看博客觉得杂乱无章,推荐按如下路径进行循序渐进的实践学习. C++ 一共 29门课 阶段 0:新手预备 新人必学 新手入门指南之玩转蓝桥云课 新手入门指南之玩转蓝桥云课 本课 ...
- 《ROS理论与实践》学习笔记(九)机器人自主导航
<ROS理论与实践>学习笔记(九)机器人自主导航 课程内容 ROS中的导航框架 导航框架中的关键功能包 move_base amcl 机器人自主导航案例 导航仿真 程序接口 move_ba ...
- V-rep机器人仿真软件使用的学习笔记
本博客同时发布于个人主页:www.doctorsrn.cn 本博客续:V-rep机器人仿真软件使用的学习笔记-续 相关博客:V-rep中机械臂惯性参数的获取方法 前言 这篇博客主要是记录自己学习和使用 ...
- 炼数成金Tensorflow学习笔记之2.2_变量
炼数成金Tensorflow学习笔记之2.2_变量 代码及分析 代码及分析 import tensorflow as tfx = tf.Variable([1, 2]) a = tf.constant ...
- Python学习笔记D2(条件与循环)
Python学习笔记D2(条件与循环) 条件语句 if expression:(可以利用布尔操作符and,or,not) *****语句- elif:(else if) ***** 语句- else: ...
- 蓝桥ROS机器人之曾经新版本(indigo/kinetic/melodic/ardent/dashing)
在蓝桥ros中曾经开发了5个版本的ROS学习教程(均在2019年之前完成).但是大部分内容并没有公开,测试多轮反馈也不好,这里供需要的朋友参考吧. ubuntu 14.04 ubuntu 16.04 ...
- python计算机器人运动学分析_V-rep学习笔记:机器人逆运动学数值解法(The Jacobian Transpose Method)...
机器人运动学逆解的问题经常出现在动画仿真和工业机器人的轨迹规划中:We want to know how the upper joints of the hierarchy would rotate ...
- 蓝桥ROS机器人课程和需求
这一类课程需求量其实并不大. 从已经开设的课程中可以看到总人数也就1.6万+ 关于ROS机器人课程维护,由于时间关系都还是基础类课程. 并且课程设置目标中性,只是提供一个云端环境供学生实验和学习用,不 ...
- ROS入门21讲 | ROS机器人入门教程 【简明笔记】
古月·ROS入门21讲 | 一学就会的ROS机器人入门教程 文章目录 ROS核心概念 ROS命令行 工作空间与功能包 订阅与发布 发布者 Publisher 订阅者 Subscriber 话题消息的自 ...
最新文章
- 这个美国议员候选人想发币,联邦选举委员会还答应了
- 修改MySQL日志文件位置
- oracle 快速备份表数据
- flink sql udf jar包_flink教程flink 1.11 集成zeppelin实现简易实时计算平台
- rat/rats--用有理数形式表示矩阵
- .NET 6 中的七个 System.Text.Json 特性
- 做题不如巧做题,初中数学题型解题技巧都在这!
- vue路由1.0_【Vue】路由
- js in html5,CSS-in-JS 来做的 5 件事情,一般人都不知道!
- CVPR2021 | 视频超分辨率中时空蒸馏方案
- 以太坊白皮书(中文版)
- 编程累了进来听听音乐
- C语言课设分享:图片加载
- AdGuard更多规则推荐
- 高德地图实现展示多个规划的路线
- chmod -R 777使用.
- UART串行通信模式
- 小米5x的android版本,小米5X是什么系统_小米5X系统更新-太平洋IT百科
- 20181207股票复盘
- RHCSA之账户与安全_创建账户与组(useradd.groupadd.id)
热门文章
- Java输入1~12之间的整数,显示该月份的英语单词及这个月属第几季度。
- python 图像分割_Python中的图像分割(第2部分)
- 【排序算法】图解冒泡排序(多图+解决两种无效比较问题)
- Linux脚本开头#!/bin/bash和#!/bin/sh是什么意思以及区别
- framemaker 导出word文档,利用list遍历输出换行以及保留段落格式(首行空两格)
- android hdmi拔插广播,Android_8.1插拔hdmi后,音量会变到最大
- 活动现场大屏幕互动系统(微信墙)修复版完美PHP源码,带完整素材包和详细使用文档
- SVN各个状态:A C D M G U R I具体含义
- 【springboot】SMS短信通实现手机验证码
- android 蓝牙自动连接,蓝牙自动连接实现