当前位置: 首页 > news >正文

C++实现仿安卓线程Handler、Message、Looper的功能

在java开发中,习惯使用Handler、Message来处理同步,比如对相机的操作(open、setParamters、start、stop、clost)全部抛到同一个线程处理,防止并发操作导致异常,这样保留给外部的统一接口就是安全的,无论外部哪些线程来调用,最终到控制模块都是在同一线程处理相机操作。这里提供一个C++实现的Handler Message封装,可以实现类似安卓那样的接口;

Handler类封装:


#ifndef _CPP_THREADHANDLER_H
#define _CPP_THREADHANDLER_H#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include "Message.h"class ThreadHandler {public:using TimePoint_t = std::chrono::steady_clock::time_point;using Clock_t = std::chrono::steady_clock;using MillisDuration_t = std::chrono::milliseconds;using Task = std::function<void(const Message& msg)>;ThreadHandler() : _stoped(false) { initLoop(); }~ThreadHandler() {_stoped = true;_cond.notify_all();_looper.join();}void setName(const std::string n) { this->name = n; }bool sendEmptyMessageDelay(int what, long delay_millis) {if (what < 0 || delay_millis < 0)return false;Message msg(what, delay_millis);std::unique_lock<std::mutex> lock(_queue_lock);_msg_list.push_back(msg);_msg_list.sort(std::less<Message>());_cond.notify_all();return true;}bool sendEmptyMessageDelay(int what) {return sendEmptyMessageDelay(what, 0);}bool postDelay(std::function<void()>&& f, long delay_millis) {if (f == nullptr || delay_millis < 0) {return false;}std::unique_lock<std::mutex> lock(_queue_lock);Message msg(0, delay_millis);msg.onRun(std::move(f));_msg_list.push_back(msg);_msg_list.sort(std::less<Message>());_cond.notify_all();return true;}bool post(std::function<void()>&& f) { return postDelay(std::move(f), 0); }void removeMessages(int what) {if (what < 0)return;std::unique_lock<std::mutex> lock(_queue_lock);if (!_msg_list.empty())_msg_list.remove_if([what](const Message& m) { return m.what == what; });}void removeAlls() {std::unique_lock<std::mutex> lock(_queue_lock);if (!_msg_list.empty())_msg_list.clear();printf("ThreadHandler::removeAlls name: %s",name.c_str());}void stop() {_stoped = true;_cond.notify_all();printf("ThreadHandler::stop name: %s", name.c_str());}void handleMessage(Task&& cb) { _callback = cb; }private:void dispatchMessage(const Message& msg) const {if (msg.task != nullptr) {msg.task();} else {if (msg.what < 0 || _callback == nullptr)return;_callback(msg);}}void initLoop() {_looper = std::thread([this]() {while (true) {Message msg;bool isFired = false;{std::unique_lock<std::mutex> lock(_queue_lock);if (_msg_list.empty()) {_cond.wait(lock, [this] {return _stoped || !_msg_list.empty();});} else {auto front = _msg_list.front();// 如果要when 大于 当前时间,则休眠;否则继续往下执行if (front.when > Clock_t::now()) {if (front.when > Clock_t::now() + maxSleepTime) {printf("ThreadHandler::initLoop time too long name: %s, when: ""%s ,now: %s , maxSleepTime: %s",name.c_str(),std::to_string(std::chrono::duration_cast<std::chrono::seconds>(front.when.time_since_epoch()).count()).c_str(),std::to_string(std::chrono::duration_cast<std::chrono::seconds>(Clock_t::now().time_since_epoch()).count()).c_str(),std::to_string(std::chrono::duration_cast<std::chrono::seconds>(maxSleepTime).count()).c_str());}_cond.wait_until(lock, front.when, [this] {return _stoped || (!_msg_list.empty() &&_msg_list.front().when <= Clock_t::now());});}}if (!_stoped && _msg_list.empty())continue;if (_stoped) {_msg_list.clear();return;}// List的头结点的时间小于等于当前时间,则执行头结点任务if (_msg_list.front().when <= Clock_t::now()) {msg = std::move(_msg_list.front());_msg_list.pop_front();isFired = true;}}if (isFired) {dispatchMessage(msg);}}});}bool _stoped;std::string name;std::list<Message> _msg_list;std::mutex _queue_lock;std::condition_variable _cond;std::thread _looper;Task _callback;std::chrono::minutes maxSleepTime{4};
};#endif  // _CPP_THREADHANDLER_H

Message实现类:


#ifndef _CPP_MESSAGE_H
#define _CPP_MESSAGE_H
#include <chrono>
#include <condition_variable>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <mutex>
#include <thread>class Message {public:using TimePoint_t = std::chrono::steady_clock::time_point;using Clock_t = std::chrono::steady_clock;using MillisDuration_t = std::chrono::milliseconds;int what;int m_arg1;int m_arg2;std::function<void()> task;TimePoint_t when;Message() : Message(-1, 0) {}Message(int what) : Message(what, 0) {}Message(int what, long delayMillis): what(what), when(Clock_t::now() + MillisDuration_t(delayMillis)) {task = nullptr;}Message(const Message& msg): what(msg.what), task(msg.task), when(msg.when) {}Message(Message&& msg) : what(msg.what), task(msg.task), when(msg.when) {}~Message() {}Message& operator=(const Message& msg) {this->what = msg.what;this->when = msg.when;this->task = msg.task;return *this;}Message& operator=(Message&& msg) {this->what = msg.what;this->when = msg.when;this->task = std::move(msg.task);return *this;}void setWhen(long delayMillis) {when = Clock_t::now() + MillisDuration_t(delayMillis);}void onRun(std::function<void()>&& f) { this->task = f; }bool operator>(const Message& msg) const { return (this->when > msg.when); }bool operator<(const Message& msg) const { return (this->when < msg.when); }bool operator==(const Message& msg) const {return (this->what == msg.what) && (this->task != nullptr) &&(msg.task != nullptr);}bool operator==(int what) const { return (this->what == what); }
};#endif  // _CPP_MESSAGE_H

使用方法:

//构造函数或类初始化中设置如下

mHandler.setName("classATh");

mHandler.handleMessage([&](const Message& msg) { handleMessage(msg); });

void classATh::handleMessage(const Message& msg) {

    if (msg.what == MSG_CODE_QUERY_DEVICE_INFO) {

        //

    } else if (msg.what == MSG_CODE_TRADE_INIT) {

      //

    } else if (msg.what == MSG_CODE_DEVICE_HEART_BEAT) {

        heartBeat();

    }

}

void classATh::startHearBeat() {

    mHandler.removeMessages(MSG_CODE_DEVICE_HEART_BEAT);

    mHandler.sendEmptyMessageDelay(MSG_CODE_DEVICE_HEART_BEAT, 60 * 1000);

}

相关文章:

C++实现仿安卓线程Handler、Message、Looper的功能

在java开发中&#xff0c;习惯使用Handler、Message来处理同步&#xff0c;比如对相机的操作(open、setParamters、start、stop、clost)全部抛到同一个线程处理&#xff0c;防止并发操作导致异常&#xff0c;这样保留给外部的统一接口就是安全的&#xff0c;无论外部哪些线程来…...

构建安全的用户登录API:从请求验证到JWT令牌生成

构建安全的用户登录API&#xff1a;从请求验证到JWT令牌生成 为了实现这个后端POST /api/users/login端点&#xff0c;我们可以使用Node.js和Express框架&#xff0c;并结合一些常用的库如jsonwebtoken、bcrypt和express-validator来处理验证和密码校验。下面是一个完整的示例…...

状态模式:封装对象状态并改变行为的设计模式

1. 引言 在软件开发中&#xff0c;某些对象的行为会随着其内部状态的变化而变化。传统的实现方式可能需要使用大量的条件语句&#xff0c;导致代码复杂且难以维护。状态模式&#xff08;State Pattern&#xff09;提供了一种有效的方法&#xff0c;通过将状态行为封装在状态类…...

备战“双11”丨AI+物流:你的快递会有什么变化?

背景 在中国&#xff0c;每天有数以亿计的包裹在运输&#xff0c;尤其在电商促销季如“双十一”、“618”期间&#xff0c;快递量更是激增。快递物流行业面临人员短缺、配送效率低下和物流承载能力有限等问题。快瞳科技提供的AI识别解决方案通过智能化手段提高工作效率和配送准…...

理解为什么要有C++设计模式

什么时设计模式&#xff1f; 每一个模式描述了一个在我们周围不断重复的问题以及该问题的解决方案的核心&#xff0c;这样&#xff0c;就能一次有一次地使用该方案&#xff0c;而不必做重复劳动。 如何解决复杂性&#xff1f; 分解&#xff1a;人们面对复杂性有一个常见的做法…...

模式匹配类型

一、匹配常量 在scala中&#xff0c;模式匹配可以匹配所有的字面量&#xff0c;包括字符串&#xff0c;字符&#xff0c;数字&#xff0c;布尔值等等 def describeConst(x:Any):String x match {case "str" > "匹配字符串"case > "匹配字符&…...

每天10个vue面试题(七)

1、Vue如何监听页面url中hash变化&#xff1f; 监听 $route 的变化&#xff1a;在Vue中&#xff0c;你可以使用watch属性来监听$route的变化。当路由发生变化时&#xff0c;会执行相应的处理函数。使用 window.location.hash&#xff1a;直接读取window.location.hash的值。这…...

如何在Linux系统中使用Apache HTTP Server

如何在Linux系统中使用Apache HTTP Server Apache简介 安装Apache 在Debian/Ubuntu系统中安装 在CentOS/RHEL系统中安装 启动Apache服务 验证Apache是否正在运行 访问Apache默认页面 配置Apache虚拟主机 创建虚拟主机配置文件 示例虚拟主机配置 创建网站根目录 准备静态网站内…...

C++基于opencv的视频质量检测--画面冻结检测

文章目录 0.引言1. 原始代码分析2. 优化方案3. 优化后的代码4. 代码详细解读 0.引言 视频质量画面冻结检测已在C基于opencv4的视频质量检测中有所介绍&#xff0c;本文将详细介绍其优化版本。 1. 原始代码分析 图像抖动检测的原始代码&#xff1a; bool ScreenFreezeDetect…...

Day22 opencv图像预处理

图像预处理 在计算机视觉和图像处理领域&#xff0c;图像预处理是一个重要的步骤&#xff0c;它能够提高后续处理&#xff08;如特征提取、目标检测等&#xff09;的准确性和效率。OpenCV 提供了许多图像预处理的函数和方法&#xff0c;常见的操作包括图像空间转换、图像大小调…...

QT中的定时器与计时器

目录 QTimer QTimer 的替代方案 API QElapsedTimer API 笔者写Qt的时候经常遇到需要定时完成任务的情况。举个例子&#xff1a;我写串口通信程序的时候需要定时向下位机发送数据。或者是定时任务周期性出发&#xff08;更新时间等&#xff09; 在Qt中&#xff0c;有两个非…...

国内AI大模型学习平台

据不完全统计&#xff0c;目前&#xff0c;国内有几大AI大模型学习平台可供选择&#xff1a; 1.昇思大模型平台&#xff1a;这是一个集算法选型、创意分享、模型实验和大模型在线体验于一体的AI学习与实践社区。 2.魔搭社区&#xff1a;提供AI模型、数据集和应用的开发与探索平…...

曹操出行借助 ApsaraMQ for Kafka Serverless 提升效率,成本节省超 20%

本文整理于 2024 年云栖大会主题演讲《云消息队列 ApsaraMQ Serverless 演进》&#xff0c;杭州优行科技有限公司消息中间件负责人王智洋分享 ApsaraMQ for Kafka Serverless 助力曹操出行实现成本优化和效率提升的实践经验。 曹操出行&#xff1a;科技驱动共享出行未来 曹操…...

深入理解数据库的三范式

数据库设计中的范式&#xff08;Normal Form&#xff09;是用于规范数据存储结构、消除冗余以及保证数据一致性的重要原则。范式的概念有多种层次&#xff0c;常用的前三种称为第一范式&#xff08;1NF&#xff09;、第二范式&#xff08;2NF&#xff09;和第三范式&#xff08…...

P11233 [CSP-S 2024] 染色

P11233 [CSP-S 2024] 染色 难度&#xff1a;提高/省选-。 考点&#xff1a;DP。 题意&#xff1a; 给定 n n n 个数 A i A_i Ai​&#xff0c;对 A i A_i Ai​ 进行染色&#xff0c;只有两种颜色。设 C C C 为 A A A 染色后的数组。 如果 A i A_i Ai​ 左侧没有预期同…...

图传推流学习(敬请期待)

图传推流简介 1.RTSP、RTP与RTCP2.搭建rtsp服务器&#xff08;资源下载&#xff09;3.搭建rtsp服务器&#xff08;搭建过程&#xff09; 1.RTSP、RTP与RTCP RTSP&#xff08;Real Time Streaming Protocol&#xff09;、RTP&#xff08;Real-time Transport Protocol&#xff0…...

【JavaGuide】十大经典排序算法总结

冒泡排序 算法步骤 不断的两两比较&#xff0c;这样当前最大的元素总是会排在最后面。所以称为冒泡。 图解算法 代码实现 public static int[] bubbleSort(int[] arr) {// i是排好了几个数for (int i 1; i < arr.length; i) {// flag标记当前循环是否调整了顺序&#xff0c…...

程序中怎样用最简单方法实现写excel文档

很多开发语言都能找到excel文档读写的库&#xff0c;但是在资源极其受限的环境下开发&#xff0c;引入这些库会带来兼容性问题。因为一个小功能引入一堆库&#xff0c;我始终觉得划不来。看到有项目引用的jar包有一百多个&#xff0c;看着头麻&#xff0c;根本搞不清谁依赖谁。…...

《机器学习与人类学习:比较、融合与未来展望》

《机器学习与人类学习&#xff1a;比较、融合与未来展望》 一、引言二、机器学习的概念与发展&#xff08;一&#xff09;机器学习的定义与分类&#xff08;二&#xff09;机器学习的发展历程&#xff08;三&#xff09;机器学习的应用领域 三、人类学习的本质与过程&#xff0…...

Mysql 8.4.3LTS 的离线部署

文章目录 一、部署环境资源配置 二、下载地址版本选择 三、部署详情1. 上传安装包2. 解压软件包3. 安装mysql3.3.1 创建mysql用户与用户组3.3.2 授权安装文件夹3.3.3 安装libaio依赖 &#xff08;坑&#xff09;ubuntu24.04 中关于libaio的坑 3.3.4 初始化Mysql数据库3.3.5 编辑…...

Redis相关知识总结(缓存雪崩,缓存穿透,缓存击穿,Redis实现分布式锁,如何保持数据库和缓存一致)

文章目录 1.什么是Redis&#xff1f;2.为什么要使用redis作为mysql的缓存&#xff1f;3.什么是缓存雪崩、缓存穿透、缓存击穿&#xff1f;3.1缓存雪崩3.1.1 大量缓存同时过期3.1.2 Redis宕机 3.2 缓存击穿3.3 缓存穿透3.4 总结 4. 数据库和缓存如何保持一致性5. Redis实现分布式…...

基于Uniapp开发HarmonyOS 5.0旅游应用技术实践

一、技术选型背景 1.跨平台优势 Uniapp采用Vue.js框架&#xff0c;支持"一次开发&#xff0c;多端部署"&#xff0c;可同步生成HarmonyOS、iOS、Android等多平台应用。 2.鸿蒙特性融合 HarmonyOS 5.0的分布式能力与原子化服务&#xff0c;为旅游应用带来&#xf…...

QT3D学习笔记——圆台、圆锥

类名作用Qt3DWindow3D渲染窗口容器QEntity场景中的实体&#xff08;对象或容器&#xff09;QCamera控制观察视角QPointLight点光源QConeMesh圆锥几何网格QTransform控制实体的位置/旋转/缩放QPhongMaterialPhong光照材质&#xff08;定义颜色、反光等&#xff09;QFirstPersonC…...

淘宝扭蛋机小程序系统开发:打造互动性强的购物平台

淘宝扭蛋机小程序系统的开发&#xff0c;旨在打造一个互动性强的购物平台&#xff0c;让用户在购物的同时&#xff0c;能够享受到更多的乐趣和惊喜。 淘宝扭蛋机小程序系统拥有丰富的互动功能。用户可以通过虚拟摇杆操作扭蛋机&#xff0c;实现旋转、抽拉等动作&#xff0c;增…...

Vue3中的computer和watch

computed的写法 在页面中 <div>{{ calcNumber }}</div>script中 写法1 常用 import { computed, ref } from vue; let price ref(100);const priceAdd () > { //函数方法 price 1price.value ; }//计算属性 let calcNumber computed(() > {return ${p…...

HTML中各种标签的作用

一、HTML文件主要标签结构及说明 1. <&#xff01;DOCTYPE html> 作用&#xff1a;声明文档类型&#xff0c;告知浏览器这是 HTML5 文档。 必须&#xff1a;是。 2. <html lang“zh”>. </html> 作用&#xff1a;包裹整个网页内容&#xff0c;lang"z…...

Redis专题-实战篇一-基于Session和Redis实现登录业务

GitHub项目地址&#xff1a;https://github.com/whltaoin/redisLearningProject_hm-dianping 基于Session实现登录业务功能提交版本码&#xff1a;e34399f 基于Redis实现登录业务提交版本码&#xff1a;60bf740 一、导入黑马点评后端项目 项目架构图 1. 前期阶段2. 后续阶段导…...

Jmeter(四) - 如何在jmeter中创建网络测试计划

1.简介 如何创建基本的 测试计划来测试网站。您将创建五个用户&#xff0c;这些用户将请求发送到JMeter网站上的两个页面。另外&#xff0c;您将告诉用户两次运行测试。 因此&#xff0c;请求总数为&#xff08;5个用户&#xff09;x&#xff08;2个请求&#xff09;x&#xff…...

成工fpga(知识星球号)——精品来袭

&#xff08;如需要相关的工程文件请关注知识星球&#xff1a;成工fpga&#xff0c;https://t.zsxq.com/DMeqH&#xff0c;关注即送200GB学习资料&#xff0c;链接已置顶&#xff01;&#xff09; 《孩子都能学会的FPGA》系列是成工完成的第一个系列&#xff0c;也有一年多的时…...

【bat win系统自动运行脚本-双击启动docker及其它】

win系统自动化运行脚本 创建一个 startup.bat右键编辑&#xff0c;输入以下示例 echo off start "" "C:\Program Files\Docker\Docker\Docker Desktop.exe"timeout /t 5docker start your_container_namestart cmd /k "conda activate your_conda_e…...