ACE_Reactor实现计时器

基于ACE的定时器队列实现     定时器MMO中的作用不需要做过多的强调,对于大多数的游戏项目来说,都是按照linux内核定时器的原理自己实现的实时器队列,详细的技术描述可以见这里,已有高人将其翻译成了中文:http://www.linuxforum.net/forum/showthreaded.php?Cat=&Board=driver&Number=385224&page=0&view=collapsed&sb=5&o 阅读详情

ACE_Reactor的原理参考:http://blog.csdn.net/calmreason/article/details/20694523点击打开链接


其中ACE_Reactor的schedule_timer()方法对应的四个参数如下

schedule_timer (ACE_Event_Handler *event_handler,
                               const void *arg,
                               const ACE_Time_Value &delay,
                               const ACE_Time_Value &interval = ACE_Time_Value::zero);

第一个参数指向一个ACE_Event_Handler类,该类提供了
virtual int handle_timeout (const ACE_Time_Value &tv,
                              const void *arg)
方法,当设置的间隔时间到达时,将触发ACE_Event_Handler的   handle_timeout方法

第二个参数表传送的参数,跟handle_timeout方法的arg参数对应

第三个参数表计时器开始起作用的时间,时间类对应为ACE_Time_Value类

第四个参数表计时器间隔执行的时间,如为0,则表计时器只计时一次


#include <ace/OS.h>  
#include <ace/Reactor.h>  
#include <iostream>
class MyTimerHandler : public ACE_Event_Handler  
{  

public:  
	MyTimerHandler(int delay,int inteval)  
	{  
		//注册回调器
		this->reactor(ACE_Reactor::instance());
		//设置回调器多长时间回调一次
		this->reactor()->schedule_timer(this,  
			0,    //传递handle_timeout给的参数  
			ACE_Time_Value(delay),  
			ACE_Time_Value(inteval));
	}  
	//定时器回调函数  
	int handle_timeout (const ACE_Time_Value ¤t_time,  
		const void * = 0)  
	{  
		std::cout<<"time out :: handle_timeout() "<<std::endl;
		return 0;  
	}  
};  

int main(int argc, char *argv[])   
{  
	//创建一个定时器
	MyTimerHandler mt(1,1);  
	//捕捉定时器到时事件,到时后,执行计时器的handle_timeout()方法
	ACE_Reactor::instance ()->run_reactor_event_loop();
 
	return 0;   
}  

每隔一秒钟输出一行



基于ACE中ACE_Event_Handler的定时器管理器实现 在C++项目开发中有许多用到定时器的地方,比如各个进程间定时发心跳数据,ftp服务定时扫描文件等等。由于定时器实现一般属于框架类的东西,工作中如果业务做的比较多的话容易忘记,这里记录一下基于ACE的定时器管理器的实现。 【定时器类】 1、由于是要被管理的,所以每个定时器都需要有一个定时器Id; 2、handle_timeout函数为定时器到点时候调用的函数; 3、设置定时器ID; 4、获取定 阅读详情

相关推荐

3、ACE Reactor框架详解与应用实践

本文详细介绍了ACE Reactor框架的工作原理及其在事件驱动网络应用程序开发中的应用实践。通过日志服务器的实现示例,展示了如何利用ACE Reactor框架简化事件处理流程,并结合多线程、异步I/O和定时器管理等技术进行性能优化。同时,文章还探讨了模块化设计、异常处理、性能调优与容错机制的最佳实践,帮助开发者构建高效稳定的网络应用程序。

neovim7hacker的博客 848

ACE技术学习指南与代码示例

本文还有配套的精品资源,点击获取 简介:ACE(Adaptive Communication Environment)是一个开源跨平台C++库,提供构建可伸缩、可靠和高性能网络通信应用的工具。它支持多种操作系统和网络协议。本压缩包提供初级到高级的学习资料和代码示例,有助于开发者深入理解ACE的框架结构、事件处理、同步机制、内存管理等概念,并应用于实际项目中。无论新手还是经验...

weixin_32480007的博客 1589

ACE笔记(2) -用ACE_Reactor实现类似计时器的装置

 用ACE_Reactor实现类似计时器的装置存在一个ACE_Reactor单体实例,其有schedule_timer方法,提供了4个参数schedule_timer (ACE_Event_Handler *event_handler,                               const void *arg,                               co

589

ACE完整牛逼定时器

ACE定时器 可以自己在添加多个定时器 代码完善 注意 字符集 :更多

一个高效的定时器分析及设计

对于一个游戏而言,定时器是必须的,而它一般作为一个游戏基本公共组件,而定时器在游戏逻辑中运用是非常明显的(比如吃药回血,每几秒回血多少),而对于游戏逻辑而言需要开发一个高效率高精度(毫秒级别)的定时器。   一:分析Ace库定时器实现方式   1.Ace种定时器实现有4种,这里不具体介绍实现细节,主要介绍实现数据结构,性能。   具体的4种定时器都是从ACE_Timer_Queue_T继承,每种定时器用不同的数据结构来实现具体Timer的算法。   1)ACE_Timer_Heap定时器,根据触发时间建立一个优先级队列(一个堆数据结构)来维护所有的定时器,代价就是删除和插入过程为O(l

ACE定时器

1) ACE_Thread_Timer_Queue_Adapter类的几个关键方法 调度定时器方法

胜利者的经验并不复杂,那就是寻找人生的关键点,并在关键时刻竭尽全力 2804

[统计模型] 基于R的潜在剖面分析(LPA)

本文主要介绍基于R进行潜在剖面分析(Latent Profile Aanalysis, LPA)。 关于潜在类别分析LCA/潜在剖面分析LPA的概念和区别,可以参考一下这篇文章: 潜在类别分析LCA \潜在剖面分析LPA介绍https://www.jianshu.com/p/4c830094131e?utm_campaign=maleskine 但是此文对潜类别分析的阐释还比较生涩,而且没有具体的代码编写、模型优选等内容。本文旨在丰富LPA的内容,并希望和大家交流、共同进步。 1. 什么是潜类别分.

bigstar863的博客 1万+

ACE笔记(2)-用ACE_Reactor实现类似计时器的装置

用ACE_Reactor实现类似计时器的装置存在一个ACE_Reactor单体实例,其有schedule_timer方法,提供了4个参数schedule_timer (ACE_Event_Handler *event_handler,                               const void *arg,                               const

风 4898

极具性价比的步进电机驱动芯片-TMC2226/TMC2209使用小结

TMC2226/2209相对于TMC2660C、TMC5160来说,价格低了很多,同时功能并没有太多阉割。对比TMC5160,少了内部加减速曲线,电流足够(Ipeak = 2.8A),相对5160而言功能也很足够,所以毫无疑问TMC2226是极具性价比的步进驱动芯片。

Trinamic_TMC的博客 7043

04目标检测-Two-stage的目标检测算法

前面我们对进行了对比分析,同时上一篇我们介绍了传统算法在目标检测的原理和效果,此篇我们记录了我学习目标检测算法的深度,深入讲述一下深度学习算法在目标检测的原理和效果。原文链接:https://blog.csdn.net/qq_41946216/article/details/132800752。版权声明:本文为CSDN博主「爱吃面的猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。One-stage(YOLO和SSD系列):直接回归目标位置。

qq_41946216的博客 3658

用ACE_Reactor实现类似计时器的装置

用ACE_Reactor实现类似计时器的装置 存在一个ACE_Reactor单体实例,其有schedule_timer方法,提供了4个参数schedule_timer (ACE_Event_Handler *event_handler,                               const void *arg,                             

iPub的专栏 3162

从格雷码到多频外差:结构光相位展开实战指南(附Python仿真代码)

本文深入探讨了结构光三维测量中的相位展开技术,重点对比了格雷码和多频外差法在工业检测、逆向工程等领域的应用。通过详细的原理图解和Python仿真代码,展示了两种方法在精度、抗噪性和适用场景上的差异,为实际工程中的技术选型提供实用指南。

weixin_42535703的博客 440

用ACE_Reactor实现类似计时器的装置收藏

用ACE_Reactor实现类似计时器的装置 存在一个ACE_Reactor单体实例,其有schedule_timer方法,提供了4个参数 schedule_timer (ACE_Event_Handler *event_handler,                                const void *arg,

Mirage520的专栏 681

【AI平台】n8n入门2:第一个工作流,调用本地大模型

n8n是一款开源的,专注于AI工作流构建,支持灵活的自定义与集成。就是可以把大模型和其他工具联合起来,这就厉害了。而且,免费功能又强大,目前很火,来研究一下。

正经教主是个热衷于正经学习分享的教主。 4547

华为面试手撕真题【判断是ipv4还是ipv6地址】

编写一个函数来验证输入的字符串是否是有效的 IPv4 或 IPv6 地址。

MISAYAONE的博客 1万+

ACE_Reactor系列

ACE_Reactor实现计时器 ACE_Reactor实现运行时接收键盘输入

C++ 爱好者 make_it_simple888 2578

微信小程序 - 解决微信支付报错 requestPayment:fail no permission 原因分析及排查处理(支付失败出错,无法唤起支付弹框面板,微信支付API报错请求付款:失败无权限)

微信小程序,微信支付,支付报错,errMsg: "requestPayment:fail no permission",支付失败,支付权限没问题,微信小程序支付报错,检查了appid正确依旧不行,微信小程序支付异常,苹果ios手机,安卓手机,开发者工具模拟器,最新解决教程,小程序wx.requestPayment报错/无响应,小程序支付 requestPaym,微信小程序认证,真机运行,小程序微信支付提示错误,微信小程序里使用微信支付错误提示,微信小程序支付异常,微信支付弹框拉不起来,小程序支付api,都检

🏆 前端领域优质创作者 1万+

ACE Reactor FrameWork使用注意事项

Ace Reactor framework 是ACE 提供的一个相对比较简单的网络服务器框架,它简化了基于事件驱动的应用程序的开发,这些程序包括了网络应用程序。这些应用程序中的事件一般包括 因io操作而产生的进程间通信流,Posix 信号,Windows 句柄信号,计时器过期信号。ACE Reacttor  framework 负责 发现各种事件源产生的事件, 把各种事件分用到先前已经注册好的事件

hermes_lch的专栏 469

UDS自动化测试-Service 0x27(CAPL调用dll实现key计算)

相信读者基于Diagnostic/ISO TP Confighration中cdd、dll文件的测试并不陌生,本文基于其的基础上,介绍在capl中调用内置的函数diagGenerateKeyFromSeed实现27服务解锁,从而进行诊断自动化测试。本文基于4个byte的Seed、16个byte的Key的安全访问,实现27服务的自动化测试。需要加载项目使用的cdd文件,如果没有的情况下可以使用canoe例程中默认的cdd文件。

诊断协议那些事儿's blog 2056

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ACE 定时器 不回调handle_timeout的问题

代码如下 #include <iostream> #include <ace/Event_Handler> #include <ace/Reactor.h> #include <string> class TestEventHandler: public ACE_Event_Handler { public: void start() { ACE_Time_Value tv(3, 0); ACE_Reacto

843

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

ACE 模式

ACE 模式

Welcom to zougangx's blog 2047

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

ACE_REACTOR的使用-I

  现在,对ACE的研究到了其事件和多线程阶段了,先从一个简单的例子入手,看: #define WIN32#define _DEBUG#include "ace/Reactor.h"#include "ace/Service_Config.h"#include "ace/OS_main.h"#include "ace/Event_Handler.h"#pragma comment(lib,

1209

调整系统时钟导致ACE定时器丢失

11         调整系统时钟导致ACE定时器丢失由于我们采用的服务器一般都是靠纽扣电池作为能源驱动和记录时钟,一般在运行一段时间后都会出现时间误差。所以很多大规模的分布系统都有校时操作,特别是一些对时钟要求精确的分布式系统(比如计费等),往往都会有一个主机提供精确时钟服务(其可能采用GPS校时),其他服务器通过这台服务器校时,校时操作一般都是直接改变系统时钟。ACE的定时器都是采用E

C++是促进脑死亡的最佳方式 3204
上一篇: 用ACE_DLL打开dll组件中的函数(6.0版)
下一篇: ACE_Service_Config动态配置服务介绍
C++程序员Carea
博客等级 码龄18年 1590粉丝 878原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值