AuroraRuntime/Include/Aurora/Threading/Threads/Spawn.hpp

40 lines
954 B
C++

/***
Copyright (C) 2023 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Spawn.hpp
Date: 2023-09-20
Author: Reece
***/
#pragma once
namespace Aurora::Threading::Threads
{
static ThreadUnique_t Spawn(AuVoidFunc entrypoint,
bool bDetached = false,
AuUInt32 uStackSize = 0)
{
auto thread = ThreadUnique(ThreadInfo(
AuMakeShared<IThreadVectorsFunctional>(IThreadVectorsFunctional::OnEntry_t(std::bind(entrypoint)), IThreadVectorsFunctional::OnExit_t {}),
uStackSize
));
if (!thread)
{
SysPushErrorNested();
return ThreadUnique_t {};
}
if (!thread->Run())
{
SysPushErrorNested();
return ThreadUnique_t {};
}
if (bDetached)
{
thread->Detach();
}
return AuMove(thread);
}
}