Refactor the RPC thread into a shutdown-able class.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 21 Mar 2016 23:21:45 +0000 (00:21 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 24 Nov 2018 10:17:41 +0000 (11:17 +0100)
src/main.cpp

index a5ed56e..39c5ce5 100644 (file)
@@ -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> server(builder.BuildAndStart());
-       std::cout << "Server listening on " << server_address << std::endl;
-       server->Wait();
-}
+       std::unique_ptr<Server> 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);