当前位置: 首页 > 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 编辑…...

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

《Playwright:微软的自动化测试工具详解》

Playwright 简介:声明内容来自网络&#xff0c;将内容拼接整理出来的文档 Playwright 是微软开发的自动化测试工具&#xff0c;支持 Chrome、Firefox、Safari 等主流浏览器&#xff0c;提供多语言 API&#xff08;Python、JavaScript、Java、.NET&#xff09;。它的特点包括&a…...

(二)TensorRT-LLM | 模型导出(v0.20.0rc3)

0. 概述 上一节 对安装和使用有个基本介绍。根据这个 issue 的描述&#xff0c;后续 TensorRT-LLM 团队可能更专注于更新和维护 pytorch backend。但 tensorrt backend 作为先前一直开发的工作&#xff0c;其中包含了大量可以学习的地方。本文主要看看它导出模型的部分&#x…...

django filter 统计数量 按属性去重

在Django中&#xff0c;如果你想要根据某个属性对查询集进行去重并统计数量&#xff0c;你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求&#xff1a; 方法1&#xff1a;使用annotate()和Count 假设你有一个模型Item&#xff0c;并且你想…...

全球首个30米分辨率湿地数据集(2000—2022)

数据简介 今天我们分享的数据是全球30米分辨率湿地数据集&#xff0c;包含8种湿地亚类&#xff0c;该数据以0.5X0.5的瓦片存储&#xff0c;我们整理了所有属于中国的瓦片名称与其对应省份&#xff0c;方便大家研究使用。 该数据集作为全球首个30米分辨率、覆盖2000–2022年时间…...

[10-3]软件I2C读写MPU6050 江协科技学习笔记(16个知识点)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16...

docker 部署发现spring.profiles.active 问题

报错&#xff1a; org.springframework.boot.context.config.InvalidConfigDataPropertyException: Property spring.profiles.active imported from location class path resource [application-test.yml] is invalid in a profile specific resource [origin: class path re…...

快刀集(1): 一刀斩断视频片头广告

一刀流&#xff1a;用一个简单脚本&#xff0c;秒杀视频片头广告&#xff0c;还你清爽观影体验。 1. 引子 作为一个爱生活、爱学习、爱收藏高清资源的老码农&#xff0c;平时写代码之余看看电影、补补片&#xff0c;是再正常不过的事。 电影嘛&#xff0c;要沉浸&#xff0c;…...

三分算法与DeepSeek辅助证明是单峰函数

前置 单峰函数有唯一的最大值&#xff0c;最大值左侧的数值严格单调递增&#xff0c;最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值&#xff0c;最小值左侧的数值严格单调递减&#xff0c;最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...

wpf在image控件上快速显示内存图像

wpf在image控件上快速显示内存图像https://www.cnblogs.com/haodafeng/p/10431387.html 如果你在寻找能够快速在image控件刷新大图像&#xff08;比如分辨率3000*3000的图像&#xff09;的办法&#xff0c;尤其是想把内存中的裸数据&#xff08;只有图像的数据&#xff0c;不包…...