.. _program_listing_file__tmp_ws_src_transport_drivers_io_context_include_io_context_io_context.hpp: Program Listing for File io_context.hpp ======================================= |exhale_lsh| :ref:`Return to documentation for file ` (``/tmp/ws/src/transport_drivers/io_context/include/io_context/io_context.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2021 LeoDrive. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Developed by LeoDrive, 2021 #ifndef IO_CONTEXT__IO_CONTEXT_HPP_ #define IO_CONTEXT__IO_CONTEXT_HPP_ #include #include #include #include "io_context/common.hpp" namespace drivers { namespace common { // Copied from https://gist.github.com/coin-au-carre/ceb8a790cec3b3535b015be3ec2a1ce2 struct thread_group { std::vector tg; thread_group() = default; thread_group(const thread_group &) = delete; thread_group & operator=(const thread_group &) = delete; thread_group(thread_group &&) = delete; template void create_thread(Args && ... args) {tg.emplace_back(std::forward(args)...);} void add_thread(std::thread && t) {tg.emplace_back(std::move(t));} std::size_t size() const {return tg.size();} void join_all() { for (auto & thread : tg) { if (thread.joinable()) { thread.join(); } } } }; class IoContext { public: IoContext(); explicit IoContext(size_t threads_count); ~IoContext(); IoContext(const IoContext &) = delete; IoContext & operator=(const IoContext &) = delete; asio::io_service & ios() const; bool isServiceStopped(); uint32_t serviceThreadCount(); void waitForExit(); template void post(F f) { ios().post(f); } private: std::shared_ptr m_ios; std::shared_ptr m_work; std::shared_ptr m_ios_thread_workers; }; } // namespace common } // namespace drivers #endif // IO_CONTEXT__IO_CONTEXT_HPP_