CMS 3D CMS Logo

LimitedTaskQueue.h
Go to the documentation of this file.
1 #ifndef FWCore_Concurrency_LimitedTaskQueue_h
2 #define FWCore_Concurrency_LimitedTaskQueue_h
3 // -*- C++ -*-
4 //
5 // Package: Concurrency
6 // Class : LimitedTaskQueue
7 //
21 //
22 // Original Author: Chris Jones
23 // Created: Thu Feb 21 11:14:39 CST 2013
24 // $Id$
25 //
26 
27 // system include files
28 #include <atomic>
29 #include <vector>
30 #include <memory>
31 
33 
34 // user include files
35 
36 // forward declarations
37 namespace edm {
39  {
40  public:
41  LimitedTaskQueue(unsigned int iLimit):
42  m_queues{iLimit}
43  { }
44 
45 
46  // ---------- member functions ---------------------------
47 
49 
55  template<typename T>
56  void push(const T& iAction);
57 
59 
66  template<typename T>
67  void pushAndWait(const T& iAction);
68 
69  private:
70  LimitedTaskQueue(const LimitedTaskQueue&) = delete;
71  const LimitedTaskQueue& operator=(const LimitedTaskQueue&) = delete;
72 
73  // ---------- member data --------------------------------
74  std::vector<SerialTaskQueue> m_queues;
75  };
76 
77  template<typename T>
78  void LimitedTaskQueue::push(const T& iAction) {
79  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
80  for(auto& q: m_queues) {
81  q.push([set_to_run,iAction]() {
82  bool expected = false;
83  if(set_to_run->compare_exchange_strong(expected,true)) {
84  iAction();
85  }
86  });
87  }
88  }
89 
90  template<typename T>
91  void LimitedTaskQueue::pushAndWait(const T& iAction) {
92  tbb::empty_task* waitTask = new (tbb::task::allocate_root()) tbb::empty_task;
93  waitTask->set_ref_count(2);
94  auto set_to_run = std::make_shared<std::atomic<bool>>(false);
95  for(auto& q: m_queues) {
96  q.push([set_to_run,waitTask,iAction]() {
97  bool expected = false;
98  if(set_to_run->compare_exchange_strong(expected,true)) {
99  try {
100  iAction();
101  }catch(...) {}
102  waitTask->decrement_ref_count();
103  }
104  });
105  }
106  waitTask->wait_for_all();
107  tbb::task::destroy(*waitTask);
108  }
109 
110 }
111 
112 #endif
def destroy(e)
Definition: pyrootRender.py:13
const LimitedTaskQueue & operator=(const LimitedTaskQueue &)=delete
std::vector< SerialTaskQueue > m_queues
void pushAndWait(const T &iAction)
synchronously pushes functor iAction into queue
LimitedTaskQueue(unsigned int iLimit)
void push(const T &iAction)
asynchronously pushes functor iAction into queue
HLT enums.
long double T