summaryrefslogtreecommitdiffhomepage
path: root/faceapi/xxx_lockfree.h
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2013-03-22 21:48:15 +0100
committerStanislaw Halik <sthalik@misaki.pl>2013-03-22 21:48:15 +0100
commit4f00c4c74d213a37a4b1a3313e50ce2b4dd51271 (patch)
treef692743cb752c994c05fe2761f83af08aa28d239 /faceapi/xxx_lockfree.h
parent5c5ec4b4238996770bfd74ddfc87934ace40bf0f (diff)
finish rename
Diffstat (limited to 'faceapi/xxx_lockfree.h')
-rw-r--r--faceapi/xxx_lockfree.h65
1 files changed, 65 insertions, 0 deletions
diff --git a/faceapi/xxx_lockfree.h b/faceapi/xxx_lockfree.h
new file mode 100644
index 00000000..ce7d2a64
--- /dev/null
+++ b/faceapi/xxx_lockfree.h
@@ -0,0 +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
+ }
+};
+