// Copyright (c) 2008-2023 the Urho3D project // License: MIT #pragma once #ifdef URHO3D_IS_BUILDING #include "Urho3D.h" #else #include #endif #ifndef _WIN32 #include using ThreadID = pthread_t; #else using ThreadID = unsigned; #endif namespace Urho3D { /// Operating system thread. class URHO3D_API Thread { public: /// Construct. Does not start the thread yet. Thread(); /// Destruct. If running, stop and wait for thread to finish. virtual ~Thread(); /// The function to run in the thread. virtual void ThreadFunction() = 0; /// Start running the thread. Return true if successful, or false if already running or if can not create the thread. bool Run(); /// Set the running flag to false and wait for the thread to finish. void Stop(); /// Set thread priority. The thread must have been started first. void SetPriority(int priority); /// Return whether thread exists. bool IsStarted() const { return handle_ != nullptr; } /// Set the current thread as the main thread. static void SetMainThread(); /// Return the current thread's ID. /// @nobind static ThreadID GetCurrentThreadID(); /// Return whether is executing in the main thread. static bool IsMainThread(); protected: /// Thread handle. void* handle_; /// Running flag. volatile bool shouldRun_; /// Main thread's thread ID. static ThreadID mainThreadID; }; }