Skip to content

Commit

Permalink
Proper gcc bug workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
anarthal committed Nov 27, 2024
1 parent 3b01d0d commit f41c70c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 11 additions & 3 deletions example/1_tutorial/6_connection_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ asio::awaitable<std::string> get_employee_details(mysql::connection_pool& pool,
// the object is destroyed.
// Fail the operation if no connection becomes available in the next 20 seconds.
mysql::pooled_connection conn = co_await pool.async_get_connection(
asio::cancel_after(std::chrono::seconds(20))
asio::cancel_after(std::chrono::seconds(1))
);
//]

Expand Down Expand Up @@ -168,15 +168,23 @@ asio::awaitable<void> listener(mysql::connection_pool& pool, unsigned short port
// Accept a new connection
auto sock = co_await acc.async_accept();

// Function implementing our session logic.
// Take ownership of the socket.
// Having this as a named variable workarounds a gcc bug
// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107288)
auto session_logic = [&pool, s = std::move(sock)]() mutable {
return handle_session(pool, std::move(s));
};

// Launch a coroutine that runs our session logic.
// We don't co_await this coroutine so we can listen
// to new connections while the session is running.
asio::co_spawn(
// Use the same executor as the current coroutine
co_await asio::this_coro::executor,

// Session logic. Take ownership of the socket
[&] { return handle_session(pool, std::move(sock)); },
// Session logic
std::move(session_logic),

// Propagate exceptions thrown in handle_session
[](std::exception_ptr ex) {
Expand Down
12 changes: 10 additions & 2 deletions example/1_tutorial/7_error_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,23 @@ asio::awaitable<void> listener(mysql::connection_pool& pool, unsigned short port
co_return;
}

// Function implementing our session logic.
// Take ownership of the socket.
// Having this as a named variable workarounds a gcc bug
// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107288)
auto session_logic = [&pool, s = std::move(sock)]() mutable {
return handle_session(pool, std::move(s));
};

// Launch a coroutine that runs our session logic.
// We don't co_await this coroutine so we can listen
// to new connections while the session is running
asio::co_spawn(
// Use the same executor as the current coroutine
co_await asio::this_coro::executor,

// Session logic. Take ownership of the socket
[&] { return handle_session(pool, std::move(sock)); },
// Session logic
std::move(session_logic),

// Will be called when the coroutine finishes
[](std::exception_ptr ptr) {
Expand Down

0 comments on commit f41c70c

Please sign in to comment.