计时器和非阻塞套接字的处理程序 [英] Timer & handler for non-blocking sockets

查看:98
本文介绍了计时器和非阻塞套接字的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要一个基于RTS的项目中的计时器实现,在该项目下,我需要执行以下操作:

1.这是一个基于套接字的项目,我正在其中发送并等待另一端的响应,该套接字是非阻塞套接字.

2.响应时间不确定,因此我们将最大响应计时器计算为5毫秒.我不能在这里使用"pselect",因为它是非阻塞套接字,也不能在其他情况下使用"usleep",否则实际进程将进入睡眠状态.

3.因此,我必须编写一个计时器,该计时器将在某个计时器处启动,并且必须在计时器到期时调用处理程序函数,但是如果在最大计时器持续时间响应之间,我们必须销毁该计时器并调用套接字处理程序函数.

4.时间和处理程序实现可能不是信号处理程序/生成器类型.

请帮助我,找到合适的方法或分享有帮助的资源.


问候,
Bisht

Hi All,

I need a timer implementation in my project which is RTS based, under which I need following:

1. It''s a socket based project in which I am sending and waiting for response from the other side, the socket is non-blocking socket.

2. The response time is not sure so we have calculated the max response timer as 5ms. I can''t use "pselect" here as it is non-blocking socket and also can''t use "usleep" other wise actual process will sleep.

3. So I have to write a timer which will started at certain timer and must call the handler function when the timer expires, but also if in between the max timer duration response comes we have to destroy the timer and call the socket handler function.

4. The time and handler implementation may or may not be of signal handler/generator type.

Please help me, either find the proper way to do it or share resources helpful for it.


Regards,
Bisht

推荐答案

很抱歉,您的问题未得到准确回答,我只想考虑一个不同的词根.

我认为自线程成为一种被广泛支持的常用技术以来,非阻塞性​​API在道德上已过时.非阻塞API等效于使用线程,但使用控件之外的线程.为什么要使用它们?对于需要为每个独立工作流程调用套接字API的每个任务,请使用单独的线程,并在每个此类线程中阻止套接字API调用;使用线程同步原语在线程之间进行通信.

在大多数情况下,它也会使计时器变得多余.

考虑一下.线程解决方案通常更显式,逻辑上更简单,容易伪造,更容易调试,整体上更可靠.
—SA
Sorry for not answering your questions exactly, I just would like to consider a different root.

I think that non-blocking APIs are rendered morally obsolete since the time when threads became a well supported commonly used techniques. Non-blocking API is equivalent to working with threads but those outside of your control. Why using them? Use separate thread for each task that require calling socket API for each independent workflow and blocking socket API calls in each of such thread; use thread synchronization primitives to communicate between threads.

It would make timers redundant, too, in most cases.

Just think about it. Threading solutions are usually more explicit, logically simpler, easily falsifiable, easier to debug, more reliable overall.

—SA


这篇关于计时器和非阻塞套接字的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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