summaryrefslogtreecommitdiff
path: root/gi/pyp-topics/src/workers.hh
diff options
context:
space:
mode:
authorbothameister <bothameister@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-06 22:38:21 +0000
committerbothameister <bothameister@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-07-06 22:38:21 +0000
commit55da6f6d4e924769cea9463c1967e4405317a8c5 (patch)
tree3c2e99a39db8aea769378949d704bf6dfafe1056 /gi/pyp-topics/src/workers.hh
parentaf33f7abb930c04c57f1422d14f976198cec6325 (diff)
Added simple multi-threading during hyperparameter resampling. Added cmdarg for controlling number of threads. Moved Timer to its own header file. Cleaned up Makefile.am
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@170 ec762483-ff6d-05da-a07a-a48fb63a330f
Diffstat (limited to 'gi/pyp-topics/src/workers.hh')
-rw-r--r--gi/pyp-topics/src/workers.hh62
1 files changed, 62 insertions, 0 deletions
diff --git a/gi/pyp-topics/src/workers.hh b/gi/pyp-topics/src/workers.hh
new file mode 100644
index 00000000..1f496acf
--- /dev/null
+++ b/gi/pyp-topics/src/workers.hh
@@ -0,0 +1,62 @@
+#ifndef WORKERS_HH
+#define WORKERS_HH
+
+#include <iostream>
+#include <boost/bind.hpp>
+#include <boost/function.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/future.hpp>
+
+//#include <boost/date_time/posix_time/posix_time_types.hpp>
+
+#include "timing.h"
+
+template <typename J, typename R>
+class SimpleWorker
+{
+typedef boost::packaged_task<R> PackagedTask;
+public:
+ SimpleWorker(J& job) : job(job), tasktime(0.0)
+ {
+ PackagedTask task(boost::bind(&SimpleWorker<J, R>::run, this));
+ future = task.get_future();
+ boost::thread t(boost::move(task));
+ }
+
+ R run() //this is called upon thread creation
+ {
+ R wresult = 0;
+
+ assert(job);
+ timer.Reset();
+ wresult = job();
+ tasktime = timer.Elapsed();
+ return wresult;
+ }
+
+ R getResult()
+ {
+ if (!future.is_ready())
+ future.wait();
+ assert(future.is_ready());
+ return future.get();
+ }
+
+ double getTaskTime()
+ {
+ return tasktime;
+ }
+
+private:
+
+ J job;
+
+ boost::unique_future<R> future;
+
+ Timer timer;
+ double tasktime;
+
+};
+
+#endif