44 lines
933 B
C
44 lines
933 B
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <thread>
|
||
|
|
#include <atomic>
|
||
|
|
#include "Containers/String.h"
|
||
|
|
|
||
|
|
namespace XCEngine {
|
||
|
|
namespace Threading {
|
||
|
|
|
||
|
|
class Thread {
|
||
|
|
public:
|
||
|
|
using Id = uint64_t;
|
||
|
|
|
||
|
|
Thread();
|
||
|
|
~Thread();
|
||
|
|
|
||
|
|
template<typename Func>
|
||
|
|
void Start(Func&& func, const Containers::String& name = "Thread");
|
||
|
|
void Join();
|
||
|
|
void Detach();
|
||
|
|
|
||
|
|
Id GetId() const { return m_id; }
|
||
|
|
const Containers::String& GetName() const { return m_name; }
|
||
|
|
|
||
|
|
static Id GetCurrentId();
|
||
|
|
static void Sleep(uint32_t milliseconds);
|
||
|
|
static void Yield();
|
||
|
|
|
||
|
|
private:
|
||
|
|
Id m_id = 0;
|
||
|
|
Containers::String m_name;
|
||
|
|
std::thread m_thread;
|
||
|
|
};
|
||
|
|
|
||
|
|
template<typename Func>
|
||
|
|
void Thread::Start(Func&& func, const Containers::String& name) {
|
||
|
|
m_name = name;
|
||
|
|
m_thread = std::thread(std::forward<Func>(func));
|
||
|
|
m_id = static_cast<Id>(reinterpret_cast<uintptr_t>(m_thread.native_handle()));
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace Threading
|
||
|
|
} // namespace XCEngine
|