ATLAS Offline Software
Loading...
Searching...
No Matches
pool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6#include "pool.h"
7
8#include <thread>
9#include <mutex>
10
11namespace {
12
13 using WorkList_cit = std::vector<WorkUnit>::const_iterator;
14
15 const WorkList_cit safe_advance(WorkList_cit& next, const WorkList_cit end)
16 {
17 static std::mutex mutex;
18 std::lock_guard<std::mutex> lock(mutex);
19 return next != end ? next++ : end;
20 }
21
22 void worker(WorkList_cit& next, const WorkList_cit end)
23 {
24 for (;;) {
25 const WorkList_cit last = safe_advance(next, end);
26 if (last == end) { return; }
27 (*last)();
28 }
29 }
30
31 struct Threads
32 {
33 Threads(const size_t n, std::function<void(void)> function)
34 {
35 for (size_t k = 0; k != n; k++) { threads.emplace_back( function ); }
36 }
37 ~Threads() { for (auto& t : threads) { t.join(); } }
38 private:
39 std::vector<std::thread> threads;
40 };
41
42} //namespace
43
44void process(const WorkList& workList, const size_t nThreads)
45{
46 WorkList_cit begin(std::begin(workList)), end(std::end(workList));
47 if (nThreads == 0) {
48 worker(begin, end);
49 } else {
50 Threads(nThreads, [&](){ worker(begin, end); });
51 }
52}
53
std::vector< WorkUnit > WorkList
STL class.
const std::string process