The Cloud Pub/Sub C++ client library will schedule parallel callbacks as long as the number of outstanding callbacks is less than this maximum.
Note that this controls the number of callbacks scheduled, not the number of callbacks actually executing at a time. The application needs to create (or configure) the background threads pool with enough parallelism to execute more than one callback at a time.
Some applications may want to share a thread pool across many subscriptions. The additional level of control (scheduled vs. running callbacks) allows applications, for example, to ensure that at most K threads in the pool are used by any given subscription.
Example
namespace pubsub = ::google::cloud::pubsub;
using ::google::cloud::future;
using ::google::cloud::GrpcBackgroundThreadPoolSizeOption;
using ::google::cloud::Options;
using ::google::cloud::StatusOr;
auto sample = [](std::string project_id, std::string subscription_id) {
// Create a subscriber with 16 threads handling I/O work, by default the
// library creates `std::thread::hardware_concurrency()` threads.
auto subscriber = pubsub::Subscriber(pubsub::MakeSubscriberConnection(
pubsub::Subscription(std::move(project_id), std::move(subscription_id)),
Options{}
.set<pubsub::MaxConcurrencyOption>(8)
.set<GrpcBackgroundThreadPoolSizeOption>(16)));
// Create a subscription where up to 8 messages are handled concurrently. By
// default the library uses `std::thread::hardware_concurrency()` as the
// maximum number of concurrent callbacks.
auto session = subscriber.Subscribe(
[](pubsub::Message const& m, pubsub::AckHandler h) {
// This handler executes in the I/O threads, applications could use,
// std::async(), a thread-pool, or any other mechanism to transfer the
// execution to other threads.
std::cout << "Received message " << m << "\n";
std::move(h).ack();
PleaseIgnoreThisSimplifiesTestingTheSamples();
});
return std::make_pair(subscriber, std::move(session));
};
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-14 UTC."],[[["\u003cp\u003eThis page details the \u003ccode\u003eMaxConcurrencyOption\u003c/code\u003e for the Cloud Pub/Sub C++ client library, which controls the maximum number of parallel callbacks scheduled.\u003c/p\u003e\n"],["\u003cp\u003eThe latest version available is \u003ccode\u003e2.37.0-rc\u003c/code\u003e, while \u003ccode\u003e2.26.0\u003c/code\u003e is presented as the current version, and the content provides a list of all versions down to \u003ccode\u003e2.11.0\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eMaxConcurrencyOption\u003c/code\u003e dictates how many outstanding callbacks can be scheduled, not the number of callbacks concurrently executing, and applications need to properly set the background thread pool size.\u003c/p\u003e\n"],["\u003cp\u003eApplications can manage thread usage by setting a maximum of \u003ccode\u003eK\u003c/code\u003e threads in a pool for any given subscription, and this is possible due to the distinction between scheduled and running callbacks.\u003c/p\u003e\n"],["\u003cp\u003eAn example is given showing how to create a subscriber with a specified maximum concurrency and thread pool size using \u003ccode\u003eMaxConcurrencyOption\u003c/code\u003e and \u003ccode\u003eGrpcBackgroundThreadPoolSizeOption\u003c/code\u003e.\u003c/p\u003e\n"]]],[],null,[]]