来自 ev_timer 的奇怪超时
Weird timeout from ev_timer
最近我在尝试使用线程代码 libev 我只是注意到计时器总是在接近 60 秒时结束,无论您将其设置为低于 60 秒。我不确定是什么原因造成的,但我尝试使代码尽可能短。
1 - 调用一些 io_init 和 io_start
2 - 创建将调用 ev_loop
的新线程
3 - 创建超时时间为 5 秒的计时器
4 - 等待线程完成然后主函数结束
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <ev.h>
void iocb(struct ev_loop *loop, ev_io *watcher, int flag){}
void tcb(struct ev_loop *loop, ev_timer *watcher, int flag){
exit(0);
}
void *loop_starter(void *loop){
ev_loop(loop, 0);
}
int main(){
struct ev_loop *loop = ev_default_loop(0);
ev_io evio;
ev_io_init(&evio, iocb, 0, EV_READ);
ev_io_start(loop, &evio);
pthread_t pid;
pthread_create(&pid, 0, loop_starter, loop);
usleep(100000); /* let's be sure it entered inside ev_loop */
ev_timer evt;
ev_timer_init(&evt, tcb, 5, 0);
ev_timer_start(loop, &evt);
pthread_join(pid, 0);
}
当我 运行 此代码与 time ./a.out
我得到
real 0m59.805s
user 0m0.000s
sys 0m0.002s
不是应该5秒后结束吗?为什么我得到的结果不是 5 秒?
我们需要实现线程互斥或 libev 的 ev_async 函数。这是使用 ev_async
的示例代码
#include <stdlib.h>
#include <pthread.h>
#include <ev.h>
void iocb(struct ev_loop *loop, ev_io *watcher, int flag){}
void tcb(struct ev_loop *loop, ev_timer *watcher, int flag){
exit(0);
}
void *loop_starter(void *loop){
ev_loop(loop, 0);
}
void async_cb(struct ev_loop *loop, ev_async *watcher, int flag){
ev_timer *evt = malloc(sizeof(ev_timer));
ev_timer_init(evt, tcb, 5, 0);
ev_timer_start(loop, evt);
}
int main(){
struct ev_loop *loop = ev_default_loop(0);
ev_async async_w;
ev_async_init(&async_w, async_cb);
ev_async_start(loop, &async_w);
ev_io evio;
ev_io_init(&evio, iocb, 0, EV_READ);
ev_io_start(loop, &evio);
pthread_t pid;
pthread_create(&pid, 0, loop_starter, loop);
/* we don't need sleep more because */
/* async_cb will be called from ev_loop */
ev_async_send(loop, &async_w);
pthread_join(pid, 0);
}
time ./a.out
real 0m5.003s
user 0m0.002s
sys 0m0.000s
最近我在尝试使用线程代码 libev 我只是注意到计时器总是在接近 60 秒时结束,无论您将其设置为低于 60 秒。我不确定是什么原因造成的,但我尝试使代码尽可能短。
1 - 调用一些 io_init 和 io_start
2 - 创建将调用 ev_loop
的新线程3 - 创建超时时间为 5 秒的计时器
4 - 等待线程完成然后主函数结束
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <ev.h>
void iocb(struct ev_loop *loop, ev_io *watcher, int flag){}
void tcb(struct ev_loop *loop, ev_timer *watcher, int flag){
exit(0);
}
void *loop_starter(void *loop){
ev_loop(loop, 0);
}
int main(){
struct ev_loop *loop = ev_default_loop(0);
ev_io evio;
ev_io_init(&evio, iocb, 0, EV_READ);
ev_io_start(loop, &evio);
pthread_t pid;
pthread_create(&pid, 0, loop_starter, loop);
usleep(100000); /* let's be sure it entered inside ev_loop */
ev_timer evt;
ev_timer_init(&evt, tcb, 5, 0);
ev_timer_start(loop, &evt);
pthread_join(pid, 0);
}
当我 运行 此代码与 time ./a.out
我得到
real 0m59.805s
user 0m0.000s
sys 0m0.002s
不是应该5秒后结束吗?为什么我得到的结果不是 5 秒?
我们需要实现线程互斥或 libev 的 ev_async 函数。这是使用 ev_async
的示例代码#include <stdlib.h>
#include <pthread.h>
#include <ev.h>
void iocb(struct ev_loop *loop, ev_io *watcher, int flag){}
void tcb(struct ev_loop *loop, ev_timer *watcher, int flag){
exit(0);
}
void *loop_starter(void *loop){
ev_loop(loop, 0);
}
void async_cb(struct ev_loop *loop, ev_async *watcher, int flag){
ev_timer *evt = malloc(sizeof(ev_timer));
ev_timer_init(evt, tcb, 5, 0);
ev_timer_start(loop, evt);
}
int main(){
struct ev_loop *loop = ev_default_loop(0);
ev_async async_w;
ev_async_init(&async_w, async_cb);
ev_async_start(loop, &async_w);
ev_io evio;
ev_io_init(&evio, iocb, 0, EV_READ);
ev_io_start(loop, &evio);
pthread_t pid;
pthread_create(&pid, 0, loop_starter, loop);
/* we don't need sleep more because */
/* async_cb will be called from ev_loop */
ev_async_send(loop, &async_w);
pthread_join(pid, 0);
}
time ./a.out
real 0m5.003s
user 0m0.002s
sys 0m0.000s