]> git.sesse.net Git - cubemap/blobdiff - thread.cpp
Factor all the common thread starting/stopping into a common Thread class.
[cubemap] / thread.cpp
diff --git a/thread.cpp b/thread.cpp
new file mode 100644 (file)
index 0000000..03bfd9d
--- /dev/null
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+
+#include "thread.h"
+
+void Thread::run()
+{
+       should_stop = false;
+       pthread_create(&worker_thread, NULL, &Thread::do_work_thunk, this);
+}
+
+void Thread::stop()
+{
+       should_stop = true;
+       pthread_kill(worker_thread, SIGHUP);
+       if (pthread_join(worker_thread, NULL) == -1) {
+               perror("pthread_join");
+               exit(1);
+       }
+}
+
+void *Thread::do_work_thunk(void *arg)
+{
+       Thread *thread = reinterpret_cast<Thread *>(arg);
+       thread->do_work();
+       return NULL;
+}
+