【问题标题】:C++ libhiredis with libev and custom event loop带有 libev 和自定义事件循环的 C++ libhiredis
【发布时间】:2020-05-08 13:26:26
【问题描述】:

我的应用程序使用带有 libev 后端的 libhiredis。我需要发送 Redis 异步命令并处理生成的 Redis 异步回调。但是,与here 中的简单示例不同,我不能使用默认事件循环。以下代码使用自定义事件循环近似示例。但是,当仅使用 redisLibevAttach() 诱导的 libev io 观察程序进行编译时,事件循环线程会立即终止。你可以通过运行看到这一点

g++ -g -std=c++11 -Wall -Wextra -Werror hiredis_ev.cpp -o hiredis_ev -lpthread -lhiredis -lev && gdb ./hiredis_ev

GDB 高兴地打印出一个新线程已创建并几乎立即终止。通过在 GDB 中运行 info thread 进一步证实了这一点,它不显示 my_ev_loop。但是,如果我更改代码以添加任何其他 libev 观察程序,例如计时器,那么一切都很好。你可以通过运行看到这一点

g++ -g -DTIMER -std=c++11 -Wall -Wextra -Werror hiredis_ev.cpp -o hiredis_ev -lpthread -lhiredis -lev &&  ./hiredis_ev

我不应该需要一个虚拟的 libev 计时器来保持事件循环运行。我错过了什么?

#include <iostream>
#include <thread>
#include <hiredis/hiredis.h>
#include <hiredis/async.h>
#include <hiredis/adapters/libev.h>

static struct ev_loop *loop = nullptr;
static void redis_async_cb(redisAsyncContext *, void *, void *)
{
  std::cout << "Redis async callback" << std::endl;
  fflush(nullptr);
}

#ifdef TIMER
  static ev_timer timer_w;
  static void ev_timer_cb(EV_P_ ev_timer *, int)
  {
    std::cout << "EV timer callback" << std::endl;
    fflush(nullptr);
  }
#endif

int main()
{
  loop = ev_loop_new(EVFLAG_AUTO);

#ifdef TIMER
  ev_timer_init(&timer_w, ev_timer_cb, 0, 0.1);
  ev_timer_start(loop, &timer_w);
#endif

  redisAsyncContext* async_context = redisAsyncConnect("localhost", 6379);
  if (nullptr == async_context)
  {
    throw std::runtime_error("No redis async context");
  }

  redisLibevAttach(loop, async_context);
  std::thread ev_thread(ev_run, loop, 0); 
  pthread_setname_np(ev_thread.native_handle(), "my_ev_loop");
  ev_thread.detach();

  // Give the event loop time to start
  while (!ev_iteration(loop))
  {
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
  }

  // Send a SUBSCRIBE message which should generate an async callback
  if (REDIS_OK != redisAsyncCommand(async_context, redis_async_cb, nullptr, "SUBSCRIBE foo"))
  {
    throw std::runtime_error("Could not issue redis async command");
  }
  std::cout << "Waiting for async callback" << std::endl;
  fflush(nullptr);
    fflush(nullptr);

  // Wait forever (use CTRL-C to terminate)
  while (true)
  {
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
  }

  return 0;
}

【问题讨论】:

    标签: c++ redis event-loop hiredis libev


    【解决方案1】:

    我发现hiredis 社区有自己的GitHub 实例,我可以在其中提问。由于我在这里还没有收到答案,所以我在那里问。答案在https://github.com/redis/hiredis/issues/801#issuecomment-626400959

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多