ATLAS Offline Software
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 
11 namespace {
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 
44 void 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 
Run3DQTestingDriver.threads
threads
Definition: Run3DQTestingDriver.py:34
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
DoubleEventSelectorOverlayTest.nThreads
nThreads
Definition: DoubleEventSelectorOverlayTest.py:83
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
process
void process(const WorkList &workList, const size_t nThreads)
Definition: pool.cxx:44
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
WorkList
std::vector< WorkUnit > WorkList
Definition: PhysicsAnalysis/D3PDTools/EventLoopGrid/Root/pool.h:13
fillPileUpNoiseLumi.next
next
Definition: fillPileUpNoiseLumi.py:52
beamspotman.n
n
Definition: beamspotman.py:731
pool.h
fitman.k
k
Definition: fitman.py:528