diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2013-09-14 15:34:44 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2013-09-14 15:34:44 +0200 |
commit | c58c0af311892929dbce4e5437c4035214552438 (patch) | |
tree | 2a6d78d740b2a83f7fe5822068bbb006a3e8a8a9 /faceapi/lockfree.h | |
parent | e695bca32e6f34461dfa720a2b693835adbb9422 (diff) |
Run dos2unix on the tree. No user-facing changes.
Diffstat (limited to 'faceapi/lockfree.h')
-rw-r--r-- | faceapi/lockfree.h | 130 |
1 files changed, 65 insertions, 65 deletions
diff --git a/faceapi/lockfree.h b/faceapi/lockfree.h index ce7d2a64..47b810fa 100644 --- a/faceapi/lockfree.h +++ b/faceapi/lockfree.h @@ -1,65 +1,65 @@ -//lock free queue template class by Herb Sutter
-//Dr Dobbs Journal article http://www.drdobbs.com/cpp/210604448;jsessionid=OQGQPSMNL4X4XQE1GHPSKH4ATMY32JVN?pgno=1
-
-template <typename T> class LockFreeQueue
-{
-private:
- struct Node
- {
- Node( T val ) : value(val), next(nullptr) { }
- T value;
- Node* next;
- };
-
- Node* first; // for producer only
- Node* divider, last; // shared
-
- //not working in VC2008
- //atomic<Node*> divider, last; // shared
-
-public:
- LockFreeQueue()
- {
- // add dummy separator
- first = divider = last = new Node( T() );
- }
-
- ~LockFreeQueue()
- {
- while( first != nullptr )
- {
- // release the list
- Node* tmp = first;
- first = tmp->next;
- delete tmp;
- }
- }
-
- void Produce( const T& t )
- {
- last->next = new Node(t); // add the new item
- last = last->next; // publish it
-
- while( first != divider )
- {
- // trim unused nodes
- Node* tmp = first;
- first = first->next;
- delete tmp;
- }
- }
-
- bool Consume( T& result )
- {
- if( divider != last )
- {
- // if queue is nonempty
- result = divider->next->value; // copy it back
- divider = divider->next; // publish that we took it
- return true; // and report success
- }
-
- return false; // else report empty
- }
-};
-
+//lock free queue template class by Herb Sutter +//Dr Dobbs Journal article http://www.drdobbs.com/cpp/210604448;jsessionid=OQGQPSMNL4X4XQE1GHPSKH4ATMY32JVN?pgno=1 + +template <typename T> class LockFreeQueue +{ +private: + struct Node + { + Node( T val ) : value(val), next(nullptr) { } + T value; + Node* next; + }; + + Node* first; // for producer only + Node* divider, last; // shared + + //not working in VC2008 + //atomic<Node*> divider, last; // shared + +public: + LockFreeQueue() + { + // add dummy separator + first = divider = last = new Node( T() ); + } + + ~LockFreeQueue() + { + while( first != nullptr ) + { + // release the list + Node* tmp = first; + first = tmp->next; + delete tmp; + } + } + + void Produce( const T& t ) + { + last->next = new Node(t); // add the new item + last = last->next; // publish it + + while( first != divider ) + { + // trim unused nodes + Node* tmp = first; + first = first->next; + delete tmp; + } + } + + bool Consume( T& result ) + { + if( divider != last ) + { + // if queue is nonempty + result = divider->next->value; // copy it back + divider = divider->next; // publish that we took it + return true; // and report success + } + + return false; // else report empty + } +}; + |