Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Thread.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "HAL/Platform.h"
6#include "HAL/PlatformAffinity.h"
7#include "Templates/Function.h"
8#include "Templates/SharedPointer.h"
9
10/**
11 * Simple API for system threads.
12 * Before using, please make sure you really need a new system thread. By default and in the majority of cases
13 * parallel processing should be done by TaskGraph.
14 * For richer functionality check `FRunnable`/`FRunnableThread`.
15 * It's up to user to provide a way to notify the thread function to exit on demand.
16 * Before destroying the instance it must be either `Join`ed or `Detach`ed.
17 * Example:
18 * FThread Thread{TEXT("New thread"), []() { do_something_important(); }};
19 * // ... continue in the caller thread
20 * Thread.Join();
21 * For more verbose example check `TestTypicalUseCase` in `ThreadTest.cpp`
22 */
23class FThread final
24{
25public:
26 // indicates if the thread should be forked in case the owning process is forked
28
29 /**
30 * Creates new "empty" thread object that doesn't represent a system thread
31 */
33 {}
34
35 /**
36 * Creates and immediately starts a new system thread that will execute `ThreadFunction` argument.
37 * Can return before the thread is actually started or when it already finished execution.
38 * @param ThreadName Name of the thread
39 * @param ThreadFunction The function that will be executed by the newly created thread
40 * @param StackSize The size of the stack to create. 0 means use the current thread's stack size
41 * @param ThreadPriority Tells the thread whether it needs to adjust its priority or not. Defaults to normal priority
42 * @param ThreadAffinity Tells the thread whether it needs to adjust its affinity or not. Defaults to no affinity
43 * @param IsForkable Tells the thread whether it can be forked. Defaults to NonForkable
44 */
46 TCHAR const* ThreadName,
47 TUniqueFunction<void()>&& ThreadFunction,
48 uint32 StackSize = 0,
49 EThreadPriority ThreadPriority = TPri_Normal,
50 FThreadAffinity ThreadAffinity = FThreadAffinity(),
51 EForkable IsForkable = NonForkable
52 );
53
54 // with SingleThreadTickFunction that will be executed every frame if running with `-nothreading
55 // (FPlatformProcess::SupportsMultithreading() == false)
57 TCHAR const* ThreadName,
58 TUniqueFunction<void()>&& ThreadFunction,
59 TUniqueFunction<void()>&& SingleThreadTickFunction,
60 uint32 StackSize = 0,
61 EThreadPriority ThreadPriority = TPri_Normal,
62 FThreadAffinity ThreadAffinity = FThreadAffinity(),
63 EForkable IsForkable = NonForkable
64 );
65
66 // non-copyable
67 FThread(const FThread&) = delete;
68 FThread& operator=(const FThread&) = delete;
69
70 FThread(FThread&&) = default;
71 /**
72 * Move assignment operator.
73 * Asserts if the instance is joinable.
74 */
75 FThread& operator=(FThread&& Other);
76
77 /**
78 * Destructor asserts if the instance is not joined or detached.
79 */
81
82 /**
83 * Checks if the thread object identifies an active thread of execution.
84 * A thread that has finished executing code, but has not yet been joined is still considered an active
85 * thread of execution and is therefore joinable.
86 * @see Join
87 */
88 bool IsJoinable() const;
89
90 /**
91 * Blocks the current thread until the thread identified by `this` finishes its execution.
92 * The completion of the thread identified by `this` synchronizes with the corresponding successful return from Join().
93 * No synchronization is performed on `this` itself. Concurrently calling Join() on the same FThread object
94 * from multiple threads constitutes a data race that results in undefined behavior.
95 * @see IsJoinable
96 */
97 void Join();
98
99 static constexpr uint32 InvalidThreadId = ~uint32(0);
100
101 /**
102 * @return Thread ID for this thread
103 */
104 uint32 GetThreadId() const;
105
106#if 0 // disabled as it doesn't work as intended
107
108 /**
109 * Separates the thread of execution from the thread object, allowing execution to continue independently.
110 * Any allocated resources will be freed once the thread exits.
111 * After calling detach `this` no longer owns any thread.
112 */
113 void Detach();
114
115#endif
116
117private:
118 TSharedPtr<class FThreadImpl, ESPMode::ThreadSafe> Impl; // "shared" with `FThreadImpl::Self`
119};
EThreadPriority
Definition Enums.h:5969
bool IsJoinable() const
static constexpr uint32 InvalidThreadId
Definition Thread.h:99
EForkable
Definition Thread.h:27
@ Forkable
Definition Thread.h:27
@ NonForkable
Definition Thread.h:27
TSharedPtr< class FThreadImpl, ESPMode::ThreadSafe > Impl
Definition Thread.h:118
FThread(const FThread &)=delete
FThread(TCHAR const *ThreadName, TUniqueFunction< void()> &&ThreadFunction, uint32 StackSize=0, EThreadPriority ThreadPriority=TPri_Normal, FThreadAffinity ThreadAffinity=FThreadAffinity(), EForkable IsForkable=NonForkable)
FThread(FThread &&)=default
FThread(TCHAR const *ThreadName, TUniqueFunction< void()> &&ThreadFunction, TUniqueFunction< void()> &&SingleThreadTickFunction, uint32 StackSize=0, EThreadPriority ThreadPriority=TPri_Normal, FThreadAffinity ThreadAffinity=FThreadAffinity(), EForkable IsForkable=NonForkable)
uint32 GetThreadId() const
void Join()
FThread & operator=(const FThread &)=delete
FThread()
Definition Thread.h:32
FThread & operator=(FThread &&Other)