Skip to content

so5extra 1.4 Mchains FixedSize

Yauheni Akhotnikau edited this page Jan 15, 2020 · 1 revision

Purpose

Sometimes an mchain is created to receive a fixed number of messages. And this number is known at the compile time. Something like that:

auto reply_ch = so_5::create_mchain(env,
      1u, // Just one message is expected.
      so_5::mchain_props::memory_usage_t::preallocated,
      so_5::mchain_props::overflow_reaction_t::abort_app);
so_5::send<request>(dest_mbox, reply_ch);
... // Do something...

// Wait and handle a reply from reply_ch.
so_5::receive(from(reply_ch).handle_n(1), [](const reply & r) {...});

The problem there is two allocations of memory in the call of create_mchain:

  • mchain instance is allocated dynamically;
  • the message queue with capacity of just one item is allocated for that mchain instance.

It seems that those two allocations make usage of mchains for request-reply scenarios too expensive. Because of that so_5::extra::mchains::fixed_size contains implementation of mchains with fixed-capacity message queue embedded into an mchain instance. Creation of such mchain lead to just one memory allocation instead of two.

Usage of such mchain looks like that:

#include <so_5_extra/mchains/fixed_size.hpp>

// The main SObjectizer's header file should be included separatelly.
#include <so_5/all.hpp>

...

// Just one message is expected.
auto reply_ch = so_5::extra::mchains::fixed_size::create_mchain<1>(env,
      so_5::mchain_props::overflow_reaction_t::abort_app);
so_5::send<request>(dest_mbox, reply_ch);
... // Do something...

// Wait and handle a reply from reply_ch.
so_5::receive(from(reply_ch).handle_n(1), [](const reply & r) {...});

The content of so_5::extra::mchains::fixed_size namespace

The so_5::extra::mchains::fixed_size namespace contains the following create_mchain functions:

// Create a mchain without waiting on attempt to send into the full mchain.
template< std::size_t Size >
[[nodiscard]] mchain_t create_mchain(
   environment_t & env,
   so_5::mchain_props::overflow_reaction_t overflow_reaction );

// Create a mchain with waiting on attempt to send into the full mchain.
template< std::size_t Size >
[[nodiscard]] mchain_t create_mchain(
   environment_t & env,
   so_5::mchain_props::duration_t wait_timeout,
   so_5::mchain_props::overflow_reaction_t overflow_reaction );

// Create a mchain from existing mchain_params object.
// Note that some values from mchain_params will be ignored.
template< std::size_t Size >
[[nodiscard]] mchain_t create_mchain(
	environment_t & env,
	const so_5::mchain_params_t & params );
Clone this wiki locally