libev 多线程下的单loop使用

开了一个线程专门跑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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。