juce AsyncUpdaterMessage 分析
这个类同样是基于 CallbackMessage, 主要目的是为了在主线程中进行回调,只不过在收到消息的时候进行检测,检测消息发送对象是否已经删除,如果消息发送对象已经没了。消息回调最终调用了调用者的 handleAsyncUpdate, 只是这样的话应当还是存在问题,主线程在处理,后台线程在删除,这样问题就大了,看来作者是没把这个问题给解决掉。
void messageCallback() override
{
if (shouldDeliver.compareAndSetBool (0, 1))
owner.handleAsyncUpdate();
}
源码码如下:
class AsyncUpdater::AsyncUpdaterMessage : public CallbackMessage
{
public:
AsyncUpdaterMessage (AsyncUpdater& au) : owner (au) {} void messageCallback() override
{
if (shouldDeliver.compareAndSetBool (0, 1))
owner.handleAsyncUpdate();
} AsyncUpdater& owner;
Atomic<int> shouldDeliver; JUCE_DECLARE_NON_COPYABLE (AsyncUpdaterMessage)
}; //==============================================================================
AsyncUpdater::AsyncUpdater()
{
activeMessage = new AsyncUpdaterMessage (*this);
} AsyncUpdater::~AsyncUpdater()
{
// You're deleting this object with a background thread while there's an update
// pending on the main event thread - that's pretty dodgy threading, as the callback could
// happen after this destructor has finished. You should either use a MessageManagerLock while
// deleting this object, or find some other way to avoid such a race condition.
jassert ((! isUpdatePending())
|| MessageManager::getInstanceWithoutCreating() == nullptr
|| MessageManager::getInstanceWithoutCreating()->currentThreadHasLockedMessageManager()); activeMessage->shouldDeliver.set (0);
} void AsyncUpdater::triggerAsyncUpdate()
{
// If you're calling this before (or after) the MessageManager is
// running, then you're not going to get any callbacks!
jassert (MessageManager::getInstanceWithoutCreating() != nullptr); if (activeMessage->shouldDeliver.compareAndSetBool (1, 0))
if (! activeMessage->post())
cancelPendingUpdate(); // if the message queue fails, this avoids getting
// trapped waiting for the message to arrive
} void AsyncUpdater::cancelPendingUpdate() noexcept
{
activeMessage->shouldDeliver.set(0);
} void AsyncUpdater::handleUpdateNowIfNeeded()
{
// This can only be called by the event thread.
jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager()); if (activeMessage->shouldDeliver.exchange (0) != 0)
handleAsyncUpdate();
} bool AsyncUpdater::isUpdatePending() const noexcept
{
return activeMessage->shouldDeliver.value != 0;
}
juce AsyncUpdaterMessage 分析的更多相关文章
- Juce源代码分析(一)Juce的优势
为什么学习Juce JUCE (Jules' Utility Class Extensions)是由Raw MaterialSoftware公布的一套基于c++的跨平台应用程序框架类库(Windows ...
- Juce源代码分析(九)应用程序基类ApplicationBase
在前面的几篇文章,分析的都是Juce库里面Core模块的内存部分,除了骨灰级C++爱好者之外,貌似大家对这些都不是非常感兴趣.相信大家更想知道Juce是怎么用于产品开发,而对于它的构成不是非常感兴趣. ...
- juce Justification 分析
很简单的一个类,一个rect放置在另一个rect中如何放置.只是没有考虑边距等,估且认为是在外层作考虑吧.然后认为是外框比内框大,所以外层怕是要进行检查才行 #ifndef JUCE_JUSTIFIC ...
- juce 中的WeakReference分析
juce中的WeakReference设计得比较巧妙,巧妙就是使用delete之后就可以通知道WeakReference,原理其实也很间单,其实就是在对象里添加了一个子对象masterReferenc ...
- juce中的timer
juce中timer总体说还是比较好用的,使用时只需继承timer类, 重写callback然后调用start就可以了,juce的timer比较特别,自己通过线程实现,starttimer的时候会创建 ...
- juce 中的ReferenceCountedObjectPtr
提供了对引用计数对象的管理,其实也就是操作引用计数对象,当引用计数为零的时候将对象销毁,值得学习的是juce是如果将引用计数对象和它的智能指针结合在一起的,这个后面再加分析 //=========== ...
- alias导致virtualenv异常的分析和解法
title: alias导致virtualenv异常的分析和解法 toc: true comments: true date: 2016-06-27 23:40:56 tags: [OS X, ZSH ...
- 火焰图分析openresty性能瓶颈
注:本文操作基于CentOS 系统 准备工作 用wget从https://sourceware.org/systemtap/ftp/releases/下载最新版的systemtap.tar.gz压缩包 ...
- 一起来玩echarts系列(一)------箱线图的分析与绘制
一.箱线图 Box-plot 箱线图一般被用作显示数据分散情况.具体是计算一组数据的中位数.25%分位数.75%分位数.上边界.下边界,来将数据从大到小排列,直观展示数据整体的分布情况. 大部分正常数 ...
随机推荐
- android onIntent 是什么东西
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...
- wcf 给net.tcp 加mex
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.s ...
- android使用友盟第三方自动更新/手动更新
最近项目用到自动更新,发现一个方便快捷的第三方组件非常好用,只需要几行代码搞定,非常适合新手使用.分享给大家一起学习. 本文档帮助您以最快速最简洁的方式集成使用自动更新组件. 一.申请友盟开发者账号创 ...
- Entity Framework排序
public ActionResult Index() { using (ApplicationDbContext db = new ApplicationDbContext()) { //var l ...
- JavaScript 原型链的一点想法
JavaScript借鉴了许多语言的特点:例如语法类Java.函数借鉴Scheme.原型继承借鉴自Self.正则表达式借鉴于Perl.(DC Javascript:语言精粹). 首先,每个J ...
- USACO Chapter 1 解题总结
USACO Chapter 1 解题总结 1.1.1 Your Ride Is Here 基本字符串操作,无压力. 1.1.2 Greedy Gift Givers 基础模拟题,弄明白题意,不怕麻烦, ...
- windows本地搭建grunt前端项目构建环境
初学,目前对grunt的理解和需求仅在于简单的文件合并.压缩.语法检查,其强大功能还有待研究. 安装前环境准备 (1)grunt依赖nodejs运行环境,所以要玩grunt得先把nodejs安装好,n ...
- DOMDocument类文件
http://php.net/manual/en/class.domdocument.php <?php $xml = <<< XML <?xml version=&qu ...
- PHP每日签到及连续签到奖励实现示例
数据库字段 num 记录已经连续签到次数 times 记录签到的日期 格式年月日 如 20160101 PHP代码如下 <?php //获取今天的日期 $today = date('Ymd'); ...
- MySQL命令记录1
mysql命令行 开启:net start mysql56关闭:net start mysql56(这两种情况必须有管理员权限) 登陆:mysql -h localhost -u root -p(lo ...