我的libevent计时器在另一个事件发生时立即停止.这正常吗?
My libevent timer stops as soon as another event occurs... is that normal?
目前我设置了两个事件:
- 监听来自客户端的新连接(我是服务器)
- 一个计时器,每秒运行一些代码来做各种后台工作
在启动时定时器工作,我可以等待只要我想,我的回调被一次又一次地调用,每秒钟就像预期的。
然而,一旦我得到一个客户端连接,另一个甚至发生,不知何故定时器停止工作。libevent接口中是否存在已知的错误,或者我是否可能滥用库?
我使用的libevent版本libevent-2.0-5:amd64在Ubuntu 14.04.
我添加了自己的c++接口,所以在这里显示所有的代码会非常大。可以在SourceForge.net的项目中浏览:
https://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/lib/snap_communicator.hhttps://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/lib/snap_communicator.cpp
的用法非常简单,但是它又非常大,完整的实现在这里:
https://sourceforge.net/p/snapcpp/code/ci/master/tree/snapwebsites/lib/snapwebsites.cpp重要的部分是这样的:
g_connection = new connection_t;
g_connection->f_communicator.reset(new snap_communicator(priority));
g_connection->f_listener.reset(new listener_impl(this, host[0].toUtf8().data(), p, max_pending_connections, true, false));
g_connection->f_listener->set_name("server listener");
g_connection->f_communicator->add_connection(g_connection->f_listener);
g_connection->f_temporary_timer.reset(new temporary_timer(this));
g_connection->f_temporary_timer->set_name("server timer");
g_connection->f_communicator->add_connection(g_connection->f_temporary_timer);
g_connection->f_communicator->run();
正如我们所看到的,我添加了这两个"连接",计时器工作得很好,直到侦听器发生。只有一个地方,我从libevent对象中删除了这两个连接,它没有命中(我知道我有一个日志,是的,日志记录器工作得很好。)
我错了!文档明确指出,您应该使套接字非阻塞。一旦我这样做了,它就开始像预期的那样工作了。
我的套接字阻塞之前,因为我只是调用accept()
…
现在我真的不明白为什么库有这样的限制,但这是解决这个问题的方法。
我为监听器添加了以下代码:
if(get_socket() != -1)
{
// libevent does not like blocking sockets...
int optval(1);
ioctl(get_socket(), FIONBIO, &optval);
}
相关文章:
- Linux的Cpp上的计时器
- 提升 ASIO 无法识别计时器对象
- 提升 asio 并发计时器取消问题与链
- 使用单体计时器的pthread_cond_timedwait有时会比预期晚超时
- 窗口中的微秒计时器
- 计时器是否从另一个线程启动?
- 如何在 c++ 中创建计时器
- C++回调计时器实现
- 在计时器或主线程外部的命令上销毁/替换线程
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 保留计时器集合(对象与指针)的最佳方法
- 在网络套接字计时器滴答后增加asio短读错误
- 是否可以仅使用标准 c++/c++11 实现不带"sleep"的计时器?
- 在没有NtSetTimerResolution的Windows上提高计时器分辨率(高分辨率)
- 计时器坏了或者其他什么的
- 功能计时器阻止主功能继续
- 我可以在Windows上使用LIBEV中的计时器吗?
- C++ 我可以在没有计时器的情况下制作 boost::asio 连接插座吗?
- 作为计时器,clock()是可靠的吗?
- 我的libevent计时器在另一个事件发生时立即停止.这正常吗?