High performance C++ framework for concurrent servers

Example use of sharded_parameter.

* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* Copyright 2020 ScyllaDB
// Demonstration of seastar::sharded_parameter
#include <seastar/core/sharded.hh>
#include <seastar/core/app-template.hh>
#include <seastar/core/thread.hh>
#include <cassert>
// This is some service that we wish to run on all shards.
class service_one {
int _capacity = 7;
// Pretend that this int is some important resource.
int get_capacity() const { return _capacity; }
// Another service that we run on all shards, that depends on service_one.
class service_two {
int _resource_allocation;
service_two(service_one& s1, int resource_allocation) : _resource_allocation(resource_allocation) {}
int get_resource_allocation() const { return _resource_allocation; }
int main(int ac, char** av) {
return, av, [&] {
// sharded<> setup code is typically run in a seastar::thread
return seastar::async([&] {
// Launch service_one
auto stop_s1 = seastar::deferred_stop(s1);
auto calculate_half_capacity = [] (service_one& s1) {
return s1.get_capacity() / 2;
// Launch service_two, passing it per-shard dependencies from s1
// Start s2, passing two parameters to service_two's constructor
// Each service_two instance will get a reference to a service_one instance on the same shard
// This calculation will be performed on each shard
seastar::sharded_parameter(calculate_half_capacity, std::ref(s1))
auto stop_s2 = seastar::deferred_stop(s2);
s2.invoke_on_all([] (service_two& s2) {
assert(s2.get_resource_allocation() == 3);
Definition: app-template.hh:47
Definition: closeable.hh:125
Helper to pass a parameter to a sharded<> object that depends on the shard. It is evaluated on the sh...
Definition: sharded.hh:537
Definition: sharded.hh:180
future invoke_on_all(smp_submit_to_options options, std::function< future<>(Service &)> func) noexcept
Definition: sharded.hh:746
future start(Args &&... args) noexcept
Definition: sharded.hh:612
futurize_t< std::invoke_result_t< Func, Args... > > async(thread_attributes attr, Func &&func, Args &&... args) noexcept
Definition: thread.hh:247
reference_wrapper< T > ref(T &object) noexcept
Wraps reference in a reference_wrapper.
Definition: reference_wrapper.hh:62