C ++定时器在Unix [英] C++ Timers in Unix

查看:119
本文介绍了C ++定时器在Unix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个处理事件计时器的API。这个API说它使用操作系统回调来处理定时事件(显然使用select())。



api声明这个执行顺序:
可读事件
可写事件
定时器事件



这是通过创建一个指向Timer对象的方法,但传递create函数a callback: / p>

以下行:

  Timer * theTimer = Timer :: Event :: create(timeInterval,& Thisclass :: FunctionName); 

我想知道这是如何工作的?

操作系统正在处理计时器本身,当它看到它被触发它是如何实际调用回调?回调是否在单独的执行线程中运行?



当我在回调函数(Thisclass :: FunctionName)中调用pthread_self()调用时,线程id作为创建本身的Timer的线程! (非常困惑的)



另外:这个优先级列表是什么意思?什么是可写事件vs可读事件?



对此场景中使用select()的任何解释也很感激。



谢谢!

解决方案

这看起来像是 select(2)的简单包装。类保留回调列表,我想分离为读,写和计时器到期。然后有一个像 dispatch wait 调用某些地方,将文件描述符包含到集合中,计算最小超时,使用这些参数调用 select 。当 select 返回时,包装器可能先经过读集,调用读回调,然后写set,然后查看任何计时器是否已过期并调用这些回调。



您应该阅读 select

code>和 poll - 它们非常方便。
一般术语是 IO解复用


We have an API that handles event timers. This API says that it uses OS callbacks to handle timed events (using select(), apparently).

The api claims this order of execution as well: readable events writable events timer events

This works by creating a point to a Timer object, but passing the create function a function callback:

Something along these lines:

Timer* theTimer =  Timer::Event::create(timeInterval,&Thisclass::FunctionName);

I was wondering how this worked?
The operating system is handling the timer itself, and when it sees it fired how does it actually invoke the callback? Does the callback run in a seperate thread of execution?

When I put a pthread_self() call inside the callback function (Thisclass::FunctionName) it appears to have the same thread id as the thread where theTimer is created itself! (Very confused by this)

Also: What does that priority list above mean? What is a writable event vs a readable event vs a timer event?

Any explanation of the use of select() in this scenario is also appreciated.

Thanks!

解决方案

This looks like a simple wrapper around select(2). The class keeps a list of callbacks, I guess separate for read, write, and timer expiration. Then there's something like a dispatch or wait call somewhere there that packs given file descriptors into sets, calculates minimum timeout, and invokes select with these arguments. When select returns, the wrapper probably goes over read set first, invoking read callback, then write set, then looks if any of the timers have expired and invokes those callbacks. This all might happen on the same thread, or on separate threads depending on the implementation of the wrapper.

You should read up on select and poll - they are very handy. The general term is IO demultiplexing.

这篇关于C ++定时器在Unix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆