]> git.sesse.net Git - cubemap/blob - thread.h
Store the client pointer directly in the epoll structure, instead of doing a map...
[cubemap] / thread.h
1 #ifndef _THREAD_H
2 #define _THREAD_H
3
4 #include <pthread.h>
5
6 // A rather generic thread class with start/stop functionality.
7 // NOTE: stop is somewhat racy (there's no guaranteed breakout from syscalls),
8 // since signals don't stick. We'll need to figure out something more
9 // intelligent later, probably based on sending a signal to an fd.
10
11 class Thread {
12 public:
13         virtual ~Thread();
14         void run();
15         void stop();
16
17 protected:
18         // Recovers the this pointer, and calls do_work().
19         static void *do_work_thunk(void *arg);
20
21         virtual void do_work() = 0;
22
23         volatile bool should_stop;
24
25         // A pipe that you can poll on if you want to see when should_stop
26         // has been set to true; stop() will write a single byte to the pipe
27         // and then close the other end.
28         int stop_fd_read;
29
30 private:
31         pthread_t worker_thread;
32         int stop_fd_write;
33 };
34
35 #endif  // !defined(_THREAD_H)