2011-04-16 36 views
0

我從一個線程開始TNonblockingServer:如何停止在C++節儉TNonblockingServer?

void *start_server(void *) { 
    server->serve(); 
    return NULL; 
} 

pthread_create(&daemon_thread, NULL, start_server, NULL); 

,並調用從主線程server->stop(),然後嘗試使用pthread_join等待後臺線程退出正常。但主線程掛在pthread_join呼叫。

如何優雅地關閉節儉服務器?

+0

這是一種預期,主線程「掛起「,而等待另一個線程終止。它是否正確地做到了,作爲停止命令的迴應? – 2011-04-16 07:41:51

+0

我期望的是,當我從主線程調用server-> stop()時,後臺線程中的server-> serve()將完成,以便此線程可以退出。因此,在主線程中使用pthread_join來等待這個後臺線程「start_server()」退出..但是「pthread_join()」會永遠掛起* – eddyxu 2011-04-16 09:34:18

回答

1

對不起,反應遲緩

你只需要停止底層的libevent

例如,稍微延遲停止:

tv.tv_usec = 500000; 
tv.tv_sec = 0; 
event_base_loopexit(myTNonBlockSvr->getEventBase(), &tv); 
0

AFAICT TNonblockingServer::stop()is not implemented。雖然TNonblockingServer析構函數確實嘗試了乾淨關閉,所以您可能能夠刪除服務器並關閉服務器。

雖然這是一個完整的黑客,理想情況下stop()將正確實施。