Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
thread.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/target_posix/roc_core/thread.h
10 //! @brief Thread.
11 
12 #ifndef ROC_CORE_THREAD_H_
13 #define ROC_CORE_THREAD_H_
14 
15 #include <pthread.h>
16 
17 #include "roc_core/atomic.h"
18 #include "roc_core/attributes.h"
19 #include "roc_core/mutex.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/stddefs.h"
22 
23 namespace roc {
24 namespace core {
25 
26 //! Base class for thread objects.
27 class Thread : public NonCopyable<Thread> {
28 public:
29  //! Get numeric identifier of current process.
30  static uint64_t get_pid();
31 
32  //! Get numeric identifier of current thread.
33  static uint64_t get_tid();
34 
35  //! Raise current thread priority to realtime.
37 
38  //! Check if thread was started and can be joined.
39  //! @returns
40  //! true if start() was called and join() was not called yet.
41  bool is_joinable() const;
42 
43  //! Start thread.
44  //! @remarks
45  //! Executes run() in new thread.
47 
48  //! Join thread.
49  //! @remarks
50  //! Blocks until run() returns and thread terminates.
51  void join();
52 
53 protected:
54  virtual ~Thread();
55 
56  Thread();
57 
58  //! Method to be executed in thread.
59  virtual void run() = 0;
60 
61 private:
62  static void* thread_runner_(void* ptr);
63 
64  pthread_t thread_;
65 
66  int started_;
67  Atomic<int> joinable_;
68 
69  Mutex mutex_;
70 };
71 
72 } // namespace core
73 } // namespace roc
74 
75 #endif // ROC_CORE_THREAD_H_
Atomic.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Mutex.
Definition: mutex.h:31
Base class for non-copyable objects.
Definition: noncopyable.h:23
Base class for thread objects.
Definition: thread.h:27
static ROC_ATTR_NODISCARD bool enable_realtime()
Raise current thread priority to realtime.
ROC_ATTR_NODISCARD bool start()
Start thread.
void join()
Join thread.
bool is_joinable() const
Check if thread was started and can be joined.
virtual void run()=0
Method to be executed in thread.
static uint64_t get_tid()
Get numeric identifier of current thread.
static uint64_t get_pid()
Get numeric identifier of current process.
Mutex.
Root namespace.
Non-copyable object.
Commonly used types and functions.