8 #ifndef WPIUTIL_WPI_SAFETHREAD_H_
9 #define WPIUTIL_WPI_SAFETHREAD_H_
16 #include "wpi/condition_variable.h"
17 #include "wpi/mutex.h"
25 virtual void Main() = 0;
27 mutable wpi::mutex m_mutex;
28 std::atomic_bool m_active{
true};
29 wpi::condition_variable m_cond;
38 explicit operator bool()
const {
return m_thread !=
nullptr; }
39 std::unique_lock<wpi::mutex>& GetLock() {
return m_lock; }
42 std::shared_ptr<SafeThread> m_thread;
43 std::unique_lock<wpi::mutex> m_lock;
53 T& operator*()
const {
return *
static_cast<T*
>(m_thread.get()); }
54 T* operator->()
const {
return static_cast<T*
>(m_thread.get()); }
78 explicit operator bool()
const;
80 std::thread::native_handle_type GetNativeThreadHandle();
82 void SetJoinAtExit(
bool joinAtExit) { m_joinAtExit = joinAtExit; }
85 void Start(std::shared_ptr<SafeThread> thr);
86 std::shared_ptr<SafeThread> GetThread()
const;
89 mutable wpi::mutex m_mutex;
90 std::thread m_stdThread;
91 std::weak_ptr<SafeThread> m_thread;
92 std::atomic_bool m_joinAtExit{
true};
102 template <
typename... Args>
103 void Start(Args&&... args) {
104 detail::SafeThreadOwnerBase::Start(
105 std::make_shared<T>(std::forward<Args>(args)...));
109 Proxy GetThread()
const {
110 return Proxy(detail::SafeThreadOwnerBase::GetThread());
116 #endif // WPIUTIL_WPI_SAFETHREAD_H_
Definition: SafeThread.h:49
Definition: SafeThread.h:22
Definition: SafeThread.h:100
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: SafeThread.h:58
Definition: SafeThread.h:35