1 year ago
#388327
Marat Idrisov
gRPC server in background thread c++ [2]
My question overlaps with grpc Server in background thread c++ in many ways. I want to run the synchronous gRPC server in a separate thread to run in the background.
This example is taken entirely from here
#include <iostream>
#include <memory>
#include <string>
#include <thread>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include "helloworld.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::Greeter;
using helloworld::HelloReply;
using helloworld::HelloRequest;
class GreeterServiceImpl final : public Greeter::Service {
Status SayHello(ServerContext* context, const HelloRequest* request,
HelloReply* reply) override {
std::string prefix("Hello ");
reply->set_message(prefix + request->name());
return Status::OK;
}
};
void RunServer() {
std::string server_address("0.0.0.0:50051");
GreeterServiceImpl service;
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
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;
std::thread([&]() {server->Wait();}); // <-- I expect the server to run in a separate thread in the background
// server->Wait();
}
int main(int argc, char** argv) {
RunServer();
std::cout << "Some work here" << std::endl;
std::cin.get();
return 0;
}
But a separate thread is not created, I get an error
/Users/marat/projects/hw/cmake-build-debug/helloworld_server
Server listening on 0.0.0.0:50051
libc++abi: terminating
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Can you tell me how to correctly start the server in the background?
c++
multithreading
grpc
0 Answers
Your Answer