![]() |
Home | Libraries | People | FAQ | More |
Boost.Asio provides a number of runtime configuration options that can be used to fine tune Boost.Asio's behaviour, such as enabling or disabling specific optimisations. The configuration options available are listed in the table below.
Section |
Key |
Type |
Default |
Description |
---|---|---|---|---|
|
|
|
|
This is a suggestion to the
When the Windows I/O completion port backend is in use, this
value is passed to
When a reactor-based backend is used, the implementation recognises
the value
No matter what value is specified for this configuration option,
the |
|
|
|
|
This is used to enable or disable locking in the scheduler, when
using a reactor-based backend. When set to
— Care must be taken to ensure that all operations on the
— Asynchronous resolve operations fail with
— If a |
|
|
|
|
The number of times to first attempt to acquire the scheduler's internal lock without blocking, when using a reactor-based backend. |
|
|
|
|
The maximum time, in microseconds, that the scheduler will wait
for its reactor task to complete. A value of |
|
|
|
|
The maximum time, in microseconds, that the scheduler will wait
on its wake-up event in an idle thread (i.e. a thread that is
not otherwise executing a handler or waiting on the reactor).
A value of |
|
|
|
|
The number of internal reactor I/O object states to allocate at construction. The reactor implementation uses per I/O object state to track things like the queue of outstanding operations. These state objects are recycled once the I/O object is destroyed, but new ones are allocated if there are no unused state objects currently available. If an upper bound on the number of I/O objects is known at construction time, this configuration option can be set to ensure that no allocations occur after construction is complete. |
|
|
|
|
Enables or disables locking in the reactor around I/O object registration and deregistration.
If set to |
|
|
|
|
The number of times to first attempt to acquire the reactor's lock without blocking, when performing I/O object registration or deregistration. |
|
|
|
|
Enables or disables per I/O object locking in the reactor
If set to |
|
|
|
|
The number of times to first attempt to acquire the reactor's per I/O object locks without blocking. |
These configuration options are associated with an execution context (such
as io_context
or thread_pool
.
In order to use non-default values, a configuration service must be installed
into the execution context at construction. A number of ways to achieve
this are illustrated in the sections below.
To read configuration options from a string, construct the execution context
with a config_from_string
object:
boost::asio::io_context my_io_context{ boost::asio::config_from_string{ "scheduler.concurrency_hint=10\n" "scheduler.locking=1"}};
Each variable must be on a line of its own, and of the form:
section.key=value
or, if an optional prefix is specified:
prefix.section.key=value
Blank lines and lines starting with #
are ignored. It is also permitted to include a comment starting with #
after the value.
To read configuration options from environment variables, construct the
execution context with a config_from_env
object:
boost::asio::io_context my_io_context{ boost::asio::config_from_env{"my_app"}};
The environment variable names are formed by concatenating the prefix,
section, and key, with underscore as delimiter, and then converting the
resulting string to upper case. For example, given a prefix "my_app"
and the "scheduler"
/ "concurrency_hint"
option,
the value
is read
from an
environment variable
named
MY_APP_SCHEDULER_CONCURRENCY_HINT`.
For backwards compatibility, the io_context
constructor can be
passed a concurrency hint as an integer. This is used to initialise the
configuration options as described in the table below.
concurrency_hint Value |
Effect |
---|---|
|
Equivalent to setting:
—
—
—
— |
|
—
—
—
— |
|
—
—
—
— |
|
—
—
—
— |
The concurrency hint used by default-constructed io_context
objects can be overridden at compile time by defining the BOOST_ASIO_CONCURRENCY_HINT_DEFAULT
macro. For example, specifying
-DBOOST_ASIO_CONCURRENCY_HINT_DEFAULT=1
on the compiler command line means that a concurrency hint of 1
is used for all default-constructed io_context
objects in
the program. Similarly, the concurrency hint used by io_context
objects constructed with 1
can be overridden by defining
BOOST_ASIO_CONCURRENCY_HINT_1
. For example, passing
-DBOOST_ASIO_CONCURRENCY_HINT_1=BOOST_ASIO_CONCURRENCY_HINT_UNSAFE
to the compiler will disable thread safety for all of these objects.
Applications and third-party libraries can utilise the config
class to associate their
own configuration options with an execution context, or to access the configuration
options listed above. The configuration parameters' values are accessed
by passing a section, key and default value to the get
member function:
boost::asio::config cfg{ctx}; bool enable_locking = cfg.get("scheduler", "locking", true);