issue#11: cds: changed __CDS_ guard prefix to CDSLIB_ for all .h files
[libcds.git] / cds / os / posix / thread.h
1 //$$CDS-header$$
2
3 #ifndef CDSLIB_OS_POSIX_THREAD_H
4 #define CDSLIB_OS_POSIX_THREAD_H
5
6 #include <pthread.h>
7 #include <signal.h>
8
9 namespace cds { namespace OS {
10     /// posix-related wrappers
11     inline namespace posix {
12
13         /// Posix thread id type
14         typedef std::thread::native_handle_type ThreadId;
15
16         /// Get current thread id
17         static inline ThreadId get_current_thread_id()
18         {
19             return pthread_self();
20         }
21
22         /// Checks if thread \p id is alive
23         static inline bool is_thread_alive( ThreadId id )
24         {
25             // if sig is zero, error checking is performed but no signal is actually sent.
26             // ESRCH - No thread could be found corresponding to that specified by the given thread ID
27             // Unresolved problem: Linux may crash on dead thread_id. Workaround unknown (except signal handler...)
28             return pthread_kill( id, 0 ) != ESRCH;
29         }
30     }    // namespace posix
31
32     //@cond
33     constexpr const posix::ThreadId c_NullThreadId = 0;
34     //@endcond
35
36 }} // namespace cds::OS
37
38
39 #endif // #ifndef CDSLIB_OS_POSIX_THREAD_H