farm-ng-core
thread_pool.h
Go to the documentation of this file.
1 // Copyright 2022, farm-ng inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Copyright (c) farm-ng, inc. All rights reserved.
16 
17 #pragma once
18 
19 //#include <boost/asio/io_context.hpp>
21 
22 #include <thread>
23 
24 namespace farm_ng {
25 
26 // NOTE: should we consider using boost::asio::thread_pool ?
27 // https://www.boost.org/doc/libs/master/doc/html/boost_asio/reference/thread_pool.html
28 
29 /// Class representing a pool of threads connected to a boost:asio::io_context.
30 class ThreadPool {
31  public:
32  /// Constructor that creates a boost:asio::io_context.
33  ThreadPool();
34  /// Constructor for an existing boost:asio::io_context.
35  ThreadPool(std::shared_ptr<Context::IoContext> ctx);
36 
37  /// Signals to the context to stop.
38  void stop();
39 
40  /// Start the threadpool, given a thread count.
41  void start(size_t n_threads);
42 
43  /// Joins the threads in the pool.
44  void join();
45 
46  /// Returns a shared pointer to the owned `io_context`.
48 
49  private:
50  std::shared_ptr<Context::IoContext> ctx_;
51  std::vector<std::thread> threads_;
52 };
53 
54 } // namespace farm_ng
farm_ng
Definition: backtrace.cpp:102
farm_ng::ThreadPool::start
void start(size_t n_threads)
Start the threadpool, given a thread count.
Definition: thread_pool.cpp:31
farm_ng::ThreadPool::getAsioIoContext
Context::IoContext & getAsioIoContext()
Returns a shared pointer to the owned io_context.
Definition: thread_pool.cpp:49
farm_ng::ThreadPool::join
void join()
Joins the threads in the pool.
Definition: thread_pool.cpp:42
farm_ng::Context::IoContext
boost::asio::io_service IoContext
Definition: context.h:47
farm_ng::ThreadPool
Class representing a pool of threads connected to a boost:asio::io_context.
Definition: thread_pool.h:30
context.h
farm_ng::ThreadPool::ThreadPool
ThreadPool()
Constructor that creates a boost:asio::io_context.
Definition: thread_pool.cpp:23
farm_ng::ThreadPool::stop
void stop()
Signals to the context to stop.
Definition: thread_pool.cpp:27