嘗試使用hiredis和libev庫編寫簡單的Redis客戶端。 一切都進展順利,除了停止事件循環 - m_thread.join()剛剛卡住。 將所有初始化的東西移動到新創建的線程中什麼也不做。Hiredis,libev and boost:threads
這裏是我的代碼部分:
void RedisSubscriber::Start() { m_redis = redisAsyncConnect(m_addr.c_str(),m_port); m_redis->data = (void*)this; m_loop = ev_loop_new(EVFLAG_NOINOTIFY); redisLibevAttach(m_loop, m_redis); redisAsyncSetConnectCallback(m_redis,connectCallback); redisAsyncSetDisconnectCallback(m_redis,disconnectCallback); redisAsyncCommand(m_redis, subscribeCallback, NULL, "SUBSCRIBE %s", m_channel.c_str()); m_thread = boost::thread(ev_loop,m_loop,0); } void RedisSubscriber::Stop() { redisAsyncFree(m_redis); m_thread.join(); m_redis = 0; } void RedisSubscriber::connectCallback(const redisAsyncContext *c) { } void RedisSubscriber::disconnectCallback(const redisAsyncContext *c, int status) { RedisSubscriber* r = (RedisSubscriber*)(c->data); ev_unloop(r->m_loop,EVUNLOOP_ALL); } void RedisSubscriber::subscribeCallback(redisAsyncContext *c, void *r, void *privdata) { }