diff --git a/content/en/docs/languages/cpp/basics.md b/content/en/docs/languages/cpp/basics.md index 93aae42..2776673 100644 --- a/content/en/docs/languages/cpp/basics.md +++ b/content/en/docs/languages/cpp/basics.md @@ -283,17 +283,17 @@ Finally, let's look at our bidirectional streaming RPC `RouteChat()`. ```cpp Status RouteChat(ServerContext* context, - ServerReaderWriter* stream) override { - std::vector received_notes; + ServerReaderWriter* stream) override { RouteNote note; while (stream->Read(¬e)) { - for (const RouteNote& n : received_notes) { + std::unique_lock lock(mu_); + for (const RouteNote& n : received_notes_) { if (n.location().latitude() == note.location().latitude() && n.location().longitude() == note.location().longitude()) { stream->Write(n); } } - received_notes.push_back(note); + received_notes_.push_back(note); } return Status::OK; @@ -307,6 +307,9 @@ get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently. +Note that since `received_notes_` is an instance variable and can be accessed by +multiple threads, we use a mutex lock here to guarantee exclusive access. + #### Starting the server Once we've implemented all our methods, we also need to start up a gRPC server