// Copyright (c) 2023 Klemens D. Morgenstern // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #if defined(BOOST_COBALT_USE_BOOST_CONTAINER_PMR) #include #endif #include #include "test.hpp" using namespace boost; BOOST_AUTO_TEST_SUITE(strand); cobalt::promise do_the_thing() { static std::atomic unique = 0; asio::steady_timer tim{co_await cobalt::this_coro::executor, std::chrono::milliseconds(50)}; co_await tim.async_wait(cobalt::use_op); unique++; BOOST_CHECK(unique == 1); unique--; } #if !defined(BOOST_COBALT_USE_IO_CONTEXT) BOOST_AUTO_TEST_CASE(strand) { std::vector ths; asio::io_context ctx; asio::any_io_executor exec{asio::make_strand(ctx.get_executor())}; #if !defined(BOOST_COBALT_NO_PMR) cobalt::pmr::synchronized_pool_resource sync; #endif for (int i = 0; i < 8; i++) ths.push_back( std::thread{ [&] { #if !defined(BOOST_COBALT_NO_PMR) cobalt::this_thread::set_default_resource(&sync); #endif cobalt::this_thread::set_executor(exec); +do_the_thing(); ctx.run(); }}); for (auto & th : ths) th.join(); } #else BOOST_AUTO_TEST_CASE(dummy) {} #endif BOOST_AUTO_TEST_SUITE_END();