-
Notifications
You must be signed in to change notification settings - Fork 0
Library Asio
Dmdv edited this page Jul 11, 2026
·
1 revision
Source:
docs/libraries/asio.mdin the main repository.
What it is: Asynchronous I/O framework — timers, sockets, strands, executors.
In this project: Homebrew asio (standalone) + Boost.Beast/Boost.Asio.
Smoke: tests/test_beast_asio.cpp, make / make run
| Flavor | Header | Notes |
|---|---|---|
| Standalone Asio | #include <asio.hpp> |
No Boost dependency; define ASIO_STANDALONE
|
| Boost.Asio | #include <boost/asio.hpp> |
Ships with Boost; Beast uses this |
This project’s CMake defines ASIO_STANDALONE for the standalone include path.
Beast still uses boost::asio / boost::beast namespaces.
-
io_context— the event loop (queue of work). - Handlers — functions run when async ops complete.
-
run()— blocks (or runs) until work is done / stopped. - Executors — where work runs (thread pool, strand for serialisation).
post/async_op ──► io_context queue ──► run() dispatches handlers
#include <asio.hpp>
#include <atomic>
#include <iostream>
int main() {
asio::io_context ioc;
std::atomic<int> n{0};
asio::post(ioc, [&] { n.fetch_add(1); });
asio::post(ioc, [&] { n.fetch_add(1); });
ioc.run(); // runs both handlers
std::cout << n.load() << "\n"; // 2
}#include <boost/asio.hpp>
#include <chrono>
#include <iostream>
namespace net = boost::asio;
int main() {
net::io_context ioc;
net::steady_timer timer(ioc, std::chrono::milliseconds(50));
timer.async_wait([](const boost::system::error_code& ec) {
if (!ec) std::cout << "tick\n";
});
ioc.run();
}#include <boost/beast/http.hpp>
#include <iostream>
namespace http = boost::beast::http;
int main() {
http::request<http::string_body> req{http::verb::get, "/health", 11};
req.set(http::field::host, "localhost");
req.set(http::field::user_agent, "lib_smoke");
req.prepare_payload();
std::cout << req.method_string() << " " << req.target() << "\n";
}#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/asio/buffer.hpp>
#include <string>
namespace beast = boost::beast;
namespace http = beast::http;
namespace net = boost::asio;
void parse() {
const std::string raw =
"HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello";
http::response_parser<http::string_body> parser;
parser.eager(true);
beast::error_code ec;
parser.put(net::const_buffer(raw.data(), raw.size()), ec);
// parser.is_done() && parser.get().body() == "hello"
}| Pattern | Purpose |
|---|---|
strand |
Serialise handlers that touch shared state |
Thread pool + io_context |
Multi-threaded server (run() on N threads) |
| Cancellation |
cancellation_signal / slots (modern Asio) |
| Coroutines |
asio::awaitable + co_spawn (C++20) |
| Beast WebSocket | Streaming feeds over WS |
-
Taskflow / TBB: CPU-heavy stages after Asio receives data (don’t block
io_contextthreads). - stdexec: Different composition model; can bridge with executors carefully.
- Folly Futures: Overlapping async style; pick one primary model per subsystem.
- HPX: Not a network stack; use for parallel compute, not as Asio replacement.
make configure && make build
./build/test_beast_asio # Catch2 Asio/Beast tests
# or
cd build && ctest -R 'Asio|Beast' --output-on-failure-
Forgetting
run()— posted work never executes. - Blocking inside a handler — starves the io_context; offload to a worker pool.
- Lifetime-free shared state without a strand/mutex.
- Mixing standalone and Boost.Asio types in the same call — don’t.
-
HPX bundling Asio — when
LIB_SMOKE_WITH_HPX=ON, prefer Homebrew Asio includes for standalone smoke (Makefile/CMake already hint Homebrew first).
Wiki mirror of repository docs. Edit docs/ in the main repo, then python3 scripts/publish_wiki.py.