Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
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/mutex.h"
20#include "roc_core/stddefs.h"
21
22namespace roc {
23namespace core {
24
25//! Base class for thread objects.
26class Thread : public NonCopyable<Thread> {
27public:
28 //! Get numeric identifier of current process.
29 static uint64_t get_pid();
30
31 //! Get numeric identifier of current thread.
32 static uint64_t get_tid();
33
34 //! Raise current thread priority to realtime.
35 static bool set_realtime();
36
37 //! Check if thread was started and can be joined.
38 //! @returns
39 //! true if start() was called and join() was not called yet.
40 bool joinable() const;
41
42 //! Start thread.
43 //! @remarks
44 //! Executes run() in new thread.
45 bool start();
46
47 //! Join thread.
48 //! @remarks
49 //! Blocks until run() returns and thread terminates.
50 void join();
51
52protected:
53 virtual ~Thread();
54
55 Thread();
56
57 //! Method to be executed in thread.
58 virtual void run() = 0;
59
60private:
61 static void* thread_runner_(void* ptr);
62
63 pthread_t thread_;
64
65 int started_;
66 Atomic<int> joinable_;
67
68 Mutex mutex_;
69};
70
71} // namespace core
72} // namespace roc
73
74#endif // ROC_CORE_THREAD_H_
Atomic.
Atomic integer. Provides sequential consistency. For a fine-grained memory order control,...
Definition atomic.h:26
Base class for non-copyable objects.
Definition noncopyable.h:23
Base class for thread objects.
Definition thread.h:26
bool start()
Start thread.
static bool set_realtime()
Raise current thread priority to realtime.
void join()
Join thread.
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.
bool joinable() const
Check if thread was started and can be joined.
Mutex.
Root namespace.
Non-copyable object.
Commonly used types and functions.