boost::capy::execution_context
Registers, looks up, and shuts down service objects owned by a derived context.
Description
An execution context represents a place where function objects are executed. It provides a service registry where polymorphic services can be stored and retrieved by type. Each service type may be stored at most once. Services may specify a nested key_type to enable lookup by a base class type.
Derived classes such as io_context extend this to provide execution facilities like event loops and thread pools. Derived class destructors must call shutdown() and destroy() to ensure proper service cleanup before member destruction. Between the two, a context that queues work abandons what is still queued, including work posted while it does so. A service created by an unwound chain is shut down as it is created, so its work is taken too. Services do not post from their destructors. A context may drain once more after destroy(), so that stray work is still destroyed, but no service may be created after destroy() returns.
Service Lifecycle
Services are created on first use via use_service() or explicitly via make_service(). During destruction, shutdown() is called on each service in reverse order of creation, then destroy() deletes them. Both functions are idempotent.
Thread Safety
Service registration and lookup functions are thread‐safe. The shutdown() and destroy() functions are not thread‐safe and must only be called during destruction.
Example
struct file_service : capy::execution_context::service
{
protected:
void shutdown() override {}
};
struct posix_file_service : file_service
{
using key_type = file_service;
explicit posix_file_service(capy::execution_context&) {}
};
class io_context : public capy::execution_context
{
public:
~io_context()
{
shutdown();
destroy();
}
};
void configure_services(io_context& ctx)
{
ctx.make_service<posix_file_service>();
ctx.find_service<file_service>(); // returns posix_file_service*
ctx.find_service<posix_file_service>(); // also works
}
Types
Name |
Description |
Gives a derived service a |
Member Functions
Name |
Description |
|
Constructors |
|
Destructor. |
|
Copy assignment is disabled; a context owns its services. |
Return a pointer to the service of type T, or nullptr. |
|
Return the memory resource used for coroutine frame allocation. |
|
Return true if a service of type T exists. |
|
Construct and add a service. |
|
|
|
Return a pointer to this context if it matches the requested type. |
|
Return a reference to the service of type T, creating it if needed. |
Protected Member Functions
Name |
Description |
|
Construct from the most‐derived context type. |
Destroy all services. |
|
Shut down all services. |
Derived Classes
Name |
Description |
Runs a work queue and event loop on the calling thread until the task completes. |
|
Distributes posted work across a fixed group of worker threads via a shared queue. |
See Also
Created with MrDocs