Belle II Software development
PThread Class Reference

Public Member Functions

template<class WORKER >
 PThread (WORKER *worker, bool destroyed=true, bool detached=true, const std::string &thread_name="")
 
pthread_t id ()
 
pthread_t get_id ()
 
bool kill (int signo)
 
bool is_alive ()
 
bool detach ()
 
bool join ()
 
bool cancel ()
 

Static Public Member Functions

static void exit ()
 

Static Private Member Functions

template<class WORKER >
static void destroy (void *arg)
 
template<class WORKER >
static void * create_destroy (void *arg)
 
template<class WORKER >
static void * create (void *arg)
 

Private Attributes

pthread_t m_th
 

Detailed Description

Definition at line 23 of file PThread.h.

Constructor & Destructor Documentation

◆ PThread() [1/2]

PThread ( )
inline

Definition at line 66 of file PThread.h.

66: m_th(0) {}

◆ PThread() [2/2]

PThread ( WORKER *  worker,
bool  destroyed = true,
bool  detached = true,
const std::string &  thread_name = "" 
)
inline

Definition at line 68 of file PThread.h.

69 {
70 m_th = 0;
71 if (destroyed) {
72 if (pthread_create(&m_th, NULL, PThread::create_destroy<WORKER>,
73 (void*)worker) != 0) {
74 m_th = 0;
75 }
76 } else {
77 if (pthread_create(&m_th, NULL, PThread::create<WORKER>,
78 (void*)worker) != 0) {
79 m_th = 0;
80 }
81 }
82#if (defined(__GLIBC__) && defined(__GNU_SOURCE) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 12)
83 if ((m_th != 0) && (thread_name != "")) {
84 try {
85 if (pthread_setname_np(m_th, thread_name.c_str()) != 0) {
86 LogFile::info("Failed to set process name %d", m_th);
87 }
88 char comp_name[64];
89 pthread_getname_np(m_th, comp_name, 16);
90 if (strcmp(thread_name.c_str(), comp_name) != 0) {
91 prctl(PR_SET_NAME, thread_name.c_str());
92 }
93 } catch (const std::exception& e) {
94 LogFile::error(e.what());
95 }
96 }
97#endif
98 if (detached) {
99 detach();
100 m_th = 0;
101 }
102 }

◆ ~PThread()

~PThread ( )
inline

Definition at line 103 of file PThread.h.

103{}

Member Function Documentation

◆ cancel()

bool cancel ( )
inline

Definition at line 124 of file PThread.h.

125 {
126 if (m_th == 0) return false;
127 return ::pthread_cancel(m_th) == 0;
128 }

◆ create()

static void * create ( void *  arg)
inlinestaticprivate

Definition at line 49 of file PThread.h.

50 {
51 WORKER* worker = (WORKER*)arg;
52 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
53 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
54 try {
55 worker->run();
56 } catch (const std::exception& e) {
57 LogFile::fatal(e.what());
58 }
59 return NULL;
60 }

◆ create_destroy()

static void * create_destroy ( void *  arg)
inlinestaticprivate

Definition at line 34 of file PThread.h.

35 {
36 WORKER* worker = (WORKER*)arg;
37 pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
38 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
39 pthread_cleanup_push(PThread::destroy<WORKER>, arg);
40 try {
41 worker->run();
42 } catch (const std::exception& e) {
43 LogFile::fatal(e.what());
44 }
45 pthread_cleanup_pop(1);
46 return NULL;
47 }

◆ destroy()

static void destroy ( void *  arg)
inlinestaticprivate

Definition at line 27 of file PThread.h.

28 {
29 WORKER* worker = (WORKER*)arg;
30 delete worker;
31 worker = NULL;
32 }

◆ detach()

bool detach ( )
inline

Definition at line 114 of file PThread.h.

115 {
116 if (m_th == 0) return false;
117 return ::pthread_detach(m_th) == 0;
118 }

◆ exit()

static void exit ( )
inlinestatic

Definition at line 63 of file PThread.h.

63{ pthread_exit(NULL); }

◆ get_id()

pthread_t get_id ( )
inline

Definition at line 107 of file PThread.h.

107{ return m_th; }

◆ id()

pthread_t id ( )
inline

Definition at line 106 of file PThread.h.

106{ return m_th; }

◆ is_alive()

bool is_alive ( )
inline

Definition at line 113 of file PThread.h.

113{ return this->kill(0); }

◆ join()

bool join ( )
inline

Definition at line 119 of file PThread.h.

120 {
121 if (m_th == 0) return false;
122 return ::pthread_join(m_th, NULL) == 0;
123 }

◆ kill()

bool kill ( int  signo)
inline

Definition at line 108 of file PThread.h.

109 {
110 if (m_th == 0) return false;
111 return ::pthread_kill(m_th, signo) == 0;
112 }

Member Data Documentation

◆ m_th

pthread_t m_th
private

Definition at line 131 of file PThread.h.


The documentation for this class was generated from the following file: