From: Steinar H. Gunderson Date: Mon, 21 Mar 2016 23:21:45 +0000 (+0100) Subject: Refactor the RPC thread into a shutdown-able class. X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=1e2e46b7190bf8ba2a3a823cca4432f3f81ae20d Refactor the RPC thread into a shutdown-able class. --- diff --git a/src/main.cpp b/src/main.cpp index a5ed56ec..39c5ce58 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -142,19 +142,26 @@ public: } } }; - -void rpc_thread() -{ - std::string server_address("0.0.0.0:50051"); - HashProbeImpl service; +class HashProbeThread { +public: + HashProbeThread(const std::string &server_address) { + builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service); + server = std::move(builder.BuildAndStart()); + std::cout << "Server listening on " << server_address << std::endl; + std::thread([this]{ server->Wait(); }).detach(); + } + + void Shutdown() { + server->Shutdown(); + } + +private: + HashProbeImpl service; ServerBuilder builder; - builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); - builder.RegisterService(&service); - std::unique_ptr server(builder.BuildAndStart()); - std::cout << "Server listening on " << server_address << std::endl; - server->Wait(); -} + std::unique_ptr server; +}; namespace PSQT { void init(); @@ -174,7 +181,7 @@ int main(int argc, char* argv[]) { Threads.set(Options["Threads"]); Search::clear(); // After threads are up - std::thread(&rpc_thread).detach(); + HashProbeThread thr("0.0.0.0:50051"); UCI::loop(argc, argv);