开了一个线程专门跑ev_run,在主线程使用ev_break 时却不能 让存在ev_run 的线程不能立即退出来, 需要等一段时间ev_run 才能退出来,一直没想明白为啥,直到搜索发现,ev_break 最好是在ev_run 的线程里调用,ev_run 有一个不确定时间的等待
ev_set_loop_release_cb (loop, void (*release)( EV_P ), void (*acquire)( EV_P ))
Sometimes you want to share the same loop between multiple threads. This can be done relatively simply by putting mutex_lock/unlock calls around each call to a libev function.
However, "ev_run" can run an indefinite time, so it is not feasible to wait for it to return. One way around this is to wake up the event loop via "ev_break" and "av_async_send", another way is to set these release and acquire callbacks on the loop.
想要立即退出 ev_run 需要在ev_run 加入ev_async, 并使用 av_async_send 唤醒, 在 ev_async的回调里面使用 ev_break就好了
参考代码见链接 using libev with multiple threads amqp-cpp-libev-stop-ev-loop-from-another-thread