JeVois  1.17
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
ThreadPool.H
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #pragma once
19 
20 #ifdef JEVOIS_PRO
21 
22 // This code modified from:
23 
24 // Copyright Mateusz Jaworski 2020 - 2020.
25 // Distributed under the Boost Software License, Version 1.0.
26 // (See accompanying file LICENSE.md or copy at
27 // https://www.boost.org/LICENSE_1_0.txt)
28 
29 #include <future>
30 #include <thread>
31 #include <vector>
32 #include <stdexcept>
33 #include <type_traits>
34 
35 #include "function2/function2.hpp"
36 #include "concurrentqueue.h"
37 
38 #include <sched.h>
39 
40 namespace jevois
41 {
42  //! A thread pool with CPU affinity
43  /*! Thread pool with extra settings to enforce that passed tasks run on some particular cores. Used by JeVois-Pro
44  Platform to enforce that machine vision tasks run on big cores (ARM A73) while non-critical tasks run on slower
45  little cores (ARM A53). \ingroup utils */
46  class ThreadPool
47  {
48  public:
49  //! Constructor
50  ThreadPool(unsigned int threads = std::thread::hardware_concurrency(), bool little = false);
51 
52  //! Destructor
53  ~ThreadPool();
54 
55  //! Execute a function and get a future
56  template<typename Func, typename... Args, std::enable_if_t<std::is_invocable_v<Func&&, Args&&...>, bool> = true>
57  auto execute(Func&& func, Args&&... args) -> std::future<decltype(func(args...))>;
58 
59  //! Get the pool size
60  auto getPoolSize() -> size_t;
61 
62  private:
63  ThreadPool& operator=(ThreadPool&) = delete;
64  ThreadPool(ThreadPool&) = delete;
65  moodycamel::ConcurrentQueue<fu2::unique_function<void()>> _tasks;
66  std::atomic<unsigned int> _size;
67  std::vector<std::thread> _pool;
68  std::condition_variable _new_task;
69  std::mutex _mtx;
70  bool _exit;
71  };
72 
73 }
74 
75 // Include implementation details
76 #include <jevois/Util/details/ThreadPoolImpl.H>
77 
78 #endif // JEVOIS_PRO
jevois::ThreadPool::getPoolSize
auto getPoolSize() -> size_t
Get the pool size.
Definition: ThreadPool.C:116
jevois::ThreadPool
A thread pool with CPU affinity.
Definition: ThreadPool.H:46
jevois
Definition: Concepts.dox:1
jevois::ThreadPool::ThreadPool
ThreadPool(unsigned int threads=std::thread::hardware_concurrency(), bool little=false)
Constructor.
Definition: ThreadPool.C:31
jevois::ThreadPool::~ThreadPool
~ThreadPool()
Destructor.
Definition: ThreadPool.C:104
jevois::ThreadPool::execute
auto execute(Func &&func, Args &&... args) -> std::future< decltype(func(args...))>
Execute a function and get a future.