LCOV - code coverage report
Current view: top level - lunchbox - threadPool.cpp (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 33 33 100.0 %
Date: 2018-10-03 05:33:11 Functions: 9 9 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2016-2017, Mohamed-Ghaith Kaabi <mohamedghaith.kaabi@gmail.com>
       2             :  *
       3             :  * This library is free software; you can redistribute it and/or modify it under
       4             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       5             :  * by the Free Software Foundation.
       6             :  *
       7             :  * This library is distributed in the hope that it will be useful, but WITHOUT
       8             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       9             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      10             :  * details.
      11             :  *
      12             :  * You should have received a copy of the GNU Lesser General Public License
      13             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      14             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      15             :  */
      16             : 
      17             : #include "threadPool.h"
      18             : 
      19             : namespace lunchbox
      20             : {
      21          66 : ThreadPool& ThreadPool::getInstance()
      22             : {
      23          66 :     static ThreadPool pool(std::thread::hardware_concurrency());
      24          66 :     return pool;
      25             : }
      26             : 
      27           4 : ThreadPool::ThreadPool(const size_t size)
      28           4 :     : _stop(false)
      29             : {
      30          28 :     for (size_t i = 0; i < size; ++i)
      31          48 :         _threads.emplace_back([this] { this->work(); });
      32           4 : }
      33             : 
      34           8 : ThreadPool::~ThreadPool()
      35             : {
      36             :     {
      37           8 :         std::unique_lock<std::mutex> lock(_mutex);
      38           4 :         _stop = true;
      39           4 :         _condition.notify_all();
      40             :     }
      41           4 :     joinAll();
      42           4 : }
      43             : 
      44          34 : size_t ThreadPool::getSize() const
      45             : {
      46          34 :     return _threads.size();
      47             : }
      48             : 
      49           2 : bool ThreadPool::hasPendingJobs() const
      50             : {
      51           4 :     std::unique_lock<std::mutex> lock(_mutex);
      52           4 :     return !_tasks.empty();
      53             : }
      54             : 
      55           4 : void ThreadPool::joinAll()
      56             : {
      57          28 :     for (auto& thread : _threads)
      58          24 :         thread.join();
      59           4 : }
      60             : 
      61          71 : void ThreadPool::work()
      62             : {
      63             :     for (;;)
      64             :     {
      65         118 :         std::function<void()> task;
      66             :         {
      67         118 :             std::unique_lock<std::mutex> lock(_mutex);
      68         181 :             _condition.wait(lock, [this] { return _stop || !_tasks.empty(); });
      69          71 :             if (_stop)
      70          48 :                 return;
      71          47 :             task = std::move(_tasks.front());
      72          47 :             _tasks.pop();
      73             :         }
      74          47 :         task();
      75          47 :     }
      76             : }
      77             : }

Generated by: LCOV version 1.11