Can I have multiple ClientAsyncReaderWriter on the same context, method and CompletionQueue?

142 Views Asked by At

In my project, two threads will call the ClientAsyncReaderWriter::Write(), but this will cause thread-safe problem.

So I try to create two ClientAsyncReaderWriter and each of them will be responsible for one thread.

But the project ended when the second ClientAsyncReaderWriter is creating.

So I wander to know if multiple ClientAsyncReaderWriter could exist simultaneously, or is there anything wrong with my creation code? Thanks a lot!

There is the associated code

 request_writer_main = client_.stub().PrepareCall(
      &ctx_, "/TraceSegmentReportService/collect", &client_.completionQueue());
  std::cout<<"request_writer_main is init"<<std::endl;

  request_writer_main->StartCall(reinterpret_cast<void*>(&ready_main));
  std::cout<<"request_writer_main is started"<<std::endl;

  request_writer_run = client_.stub().PrepareCall(
      &ctx_, "/TraceSegmentReportService/collect", &client_.completionQueue());
  std::cout<<"request_writer_run is init"<<std::endl;

  request_writer_run->StartCall(reinterpret_cast<void*>(&ready_run));
1

There are 1 best solutions below

0
ChrisMarLie On

You cannot reuse context for both rpc, so you must create another one for the second. You can check it in grpc::ClientContext:

Warning ClientContext instances should not be reused across rpcs. The ClientContext instance used for creating an rpc must remain alive and valid for the lifetime of the rpc.