3

I'm using async hiredis with libev. mLoopThread is used here for ev loop thread, basically mLoopThread is calling ev_loop(). when main thread tries to destruct async hiredis instance, it calls ev_unloop to try to make ev_loop() exit. The code looks as below. But this is not working. backtrace shows mLoopThread is hanging in epoll_wait(), and main thread is hanging in mLoopThread->join(). How to exit ev loop thread? Thanks.

~async_redis() {
    ev_unloop(mLoop, EVBREAK_ALL);
    if (mLoopThread && mLoopThread->joinable()) {
        mLoopThread->join();
    }
}
JustDance
  • 61
  • 1
  • 5

1 Answers1

4

It is difficult to answer since you only provide 4 lines of code, but it seems to me you call ev_unloop out of the event loop, which is pretty much useless.

Here, you call ev_unloop and then try to join the thread, so my understanding is you are in your main thread, want to notify the event loop to stop, and wait for the event loop thread to stop. IMO the proper way to do this is to:

  • add an ev_async handler to the event loop.

  • the callback associated to this async handler should call ev_unloop - it will execute from the event loop thread, inside the event loop.

  • in your main thread you notify the ev_async handler of the event loop (thread safe operation), and then you can join the event loop thread

This is usually how I use ev_unloop.

Didier Spezia
  • 70,911
  • 12
  • 189
  • 154