]> git.sesse.net Git - cubemap/blobdiff - thread.h
Factor all the common thread starting/stopping into a common Thread class.
[cubemap] / thread.h
diff --git a/thread.h b/thread.h
new file mode 100644 (file)
index 0000000..d9b9535
--- /dev/null
+++ b/thread.h
@@ -0,0 +1,28 @@
+#ifndef _THREAD_H
+#define _THREAD_H
+
+#include <pthread.h>
+
+// A rather generic thread class with start/stop functionality.
+// NOTE: stop is somewhat racy (there's no guaranteed breakout from syscalls),
+// since signals don't stick. We'll need to figure out something more
+// intelligent later, probably based on sending a signal to an fd.
+
+class Thread {
+public:
+       void run();
+       void stop();
+
+protected:
+       // Recovers the this pointer, and calls do_work().
+       static void *do_work_thunk(void *arg);
+
+       virtual void do_work() = 0;
+
+       volatile bool should_stop;
+
+private:
+       pthread_t worker_thread;
+};
+
+#endif  // !defined(_THREAD_H)