summaryrefslogtreecommitdiff
path: root/src/TNetLib/Barrier.h
diff options
context:
space:
mode:
authorJoe Zhao <ztuowen@gmail.com>2014-04-14 08:14:45 +0800
committerJoe Zhao <ztuowen@gmail.com>2014-04-14 08:14:45 +0800
commitcccccbf6cca94a3eaf813b4468453160e91c332b (patch)
tree23418cb73a10ae3b0688681a7f0ba9b06424583e /src/TNetLib/Barrier.h
downloadtnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.gz
tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.tar.bz2
tnet-cccccbf6cca94a3eaf813b4468453160e91c332b.zip
First commit
Diffstat (limited to 'src/TNetLib/Barrier.h')
-rw-r--r--src/TNetLib/Barrier.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/TNetLib/Barrier.h b/src/TNetLib/Barrier.h
new file mode 100644
index 0000000..a5849d2
--- /dev/null
+++ b/src/TNetLib/Barrier.h
@@ -0,0 +1,41 @@
+/*
+ * barrier.h
+ *
+ * This header file describes the "barrier" synchronization
+ * construct. The type barrier_t describes the full state of the
+ * barrier including the POSIX 1003.1c synchronization objects
+ * necessary.
+ *
+ * A barrier causes threads to wait until a set of threads has
+ * all "reached" the barrier. The number of threads required is
+ * set when the barrier is initialized, and cannot be changed
+ * except by reinitializing.
+ */
+#include <pthread.h>
+
+#ifndef barrier_h
+#define barrier_h
+
+namespace TNet {
+
+/*
+ * Structure describing a barrier.
+ */
+class Barrier {
+ public:
+ Barrier(int count=0);
+ ~Barrier();
+ void SetThreshold(int thr);
+ int Wait();
+ private:
+ pthread_mutex_t mutex_; /* Control access to barrier */
+ pthread_cond_t cv_; /* wait for barrier */
+ int threshold_; /* number of threads required */
+ int counter_; /* current number of threads */
+ int cycle_; /* alternate wait cycles (0 or 1) */
+};
+
+}//namespace TNet
+
+#endif
+