- Timestamp:
- 10/16/13 20:50:21 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/Queue.h
r17219 r17227 6 6 #include <condition_variable> 7 7 8 template<class T> 8 // The second template argument must support: 9 // iterator it = begin(); // get the next element to be processed 10 // erase(it); // erase the processed element from the queue 11 // push_back(); // add a new element to the queue 12 // emplace_back(); // emplace a new element to the queue 13 // splice(); // used to efficiently implement post with mutex 14 15 template<class T, class List=std::list<T>> 9 16 class Queue 10 17 { 11 18 size_t fSize; // Only necessary for before C++11 12 bool fSort; // Sort the list before processing 13 14 std::list<T> fList; 19 20 List fList; 15 21 16 22 std::mutex fMutex; // Mutex needed for the conditional … … 54 60 55 61 // If thread got just woken up, move back the state to kRun 56 if (fState ==kTrigger)62 if (fState==kTrigger) 57 63 fState = kRun; 58 64 … … 66 72 67 73 // get the first entry from the (sorted) list 68 const auto it = f Sort ? min_element(fList.begin(), fList.end()) : fList.begin();74 const auto it = fList.begin(); 69 75 70 76 // Theoretically, we can loose a signal here, but this is … … 76 82 // been posted (or the number of events in the queue has 77 83 // changed) [allowed>0], in the case processing was 78 // successfull [allo ed==0], the next event will be processed84 // successfull [allowed==0], the next event will be processed 79 85 // immediately. 80 86 if (!fCallback || !fCallback(*it)) … … 88 94 continue; 89 95 90 if (fSort) 91 fList.erase(it); 92 else 93 fList.pop_front() ; 96 fList.erase(it); 94 97 95 98 fSize--; … … 105 108 106 109 public: 107 Queue(const callback &f, bool s ort=false, bool startup=true) : fSize(0), fSort(sort), fState(kIdle), fCallback(f)110 Queue(const callback &f, bool startup=true) : fSize(0), fState(kIdle), fCallback(f) 108 111 { 109 112 if (startup) … … 111 114 } 112 115 113 Queue(const Queue<T >& q) : fSize(0), fSort(q.fSort), fState(kIdle), fCallback(q.fCallback)114 { 115 } 116 117 Queue<T >& operator = (const Queue<T>& q)116 Queue(const Queue<T,List>& q) : fSize(0), fState(kIdle), fCallback(q.fCallback) 117 { 118 } 119 120 Queue<T,List>& operator = (const Queue<T,List>& q) 118 121 { 119 122 fSize = 0; 120 fSort = q.fSort;121 123 fState = kIdle; 122 124 fCallback = q.fCallback; … … 229 231 230 232 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 231 bool move( std::list<T>&& x, typename std::list<T>::iterator i)233 bool move(List&& x, typename List::iterator i) 232 234 #else 233 bool move( std::list<T>& x, typename std::list<T>::iterator i)235 bool move(List& x, typename List::iterator i) 234 236 #endif 235 237 { … … 247 249 248 250 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 249 bool move( std::list<T>& x, typename std::list<T>::iterator i) { return move(std::move(x), i); }251 bool move(List& x, typename List::iterator i) { return move(std::move(x), i); } 250 252 #endif 251 253
Note:
See TracChangeset
for help on using the changeset viewer.