1 #ifndef _Belle2_Fork_hh
2 #define _Belle2_Fork_hh
18 template <
class WORKER>
19 static void g_handler_exit(
int,
void* worker)
21 delete(WORKER*)worker;
24 static void g_handler_int(
int) { exit(0); }
29 template<
class WORKER>
30 Fork(WORKER* worker,
bool detached =
true)
34 signal(SIGINT, g_handler_int);
36 on_exit(g_handler_exit<WORKER>, (
void*)worker);
40 }
else if (detached) {
47 pid_t get_id()
const {
return m_pid; }
48 void set_id(pid_t
id) { m_pid = id; }
49 bool isAlive()
const {
return kill(0); }
50 bool kill(
int signo)
const
52 if (m_pid < 0)
return false;
53 return ::kill(m_pid, signo) == 0;
55 bool wait(
int opt = 0)
57 if (m_pid < 0)
return false;
58 if (::waitpid(m_pid, NULL, opt)) {
63 bool cancel() {
return kill(SIGINT); }