final class · System.Threading
Thread
public final class Thread
A thread of its own running a function: new Thread(() => …), Start, Join.
- Kind
- final class
- Namespace
- System.Threading
- Assembly
- System.Core
- Access
- public, final
- Source file
Threading.ant
Inheritance
- Base classes
-
AnyThread
Constructors
new(func body)
#
Properties
Started and not finished.
Its number (0 until it runs): the main thread's is 1.
The number of the thread that asks.
Methods
Start()
#
Starts the thread (once).
Join()
#
Waits until the thread has finished.
Waits at most milliseconds: whether the thread has finished.
Sleeps milliseconds (other threads run).
static Yield()
#
Lets another thread run.
Waits a little without giving the processor up (in a loop that waits for another thread).
Private and internal members (12)
Inherited members (5)
GetTypemethod from AnyUndescribedmethod from AnyToStringmethod from AnyHashmethod from Anydestroymethod from Any
Source
Thread as Antril, decompiled from its Larva HIR.
// System.Threading.Thread — System.Core (decompiled from Larva HIR)
import System;
into namespace System.Threading
{
/// A thread of its own running a function: `new Thread(() => …)`, Start, Join.
public final class Thread
{
func body;
i64 handle;
/// 0 not started, 1 started, 2 joined.
i32 state;
AtomicI32 done;
i32 number;
static extern("thread.start") StartNative(func body) : i64;
static extern("thread.join") JoinNative(i64 handle, i64 milliseconds) : i64;
static extern("thread.id") CurrentNative() : i64;
static extern("thread.yield") YieldNative();
static extern("time.sleep") SleepFor(i64 milliseconds);
[NoInline]
static Misused(str what)
{
throw new InvalidOperationError("Thread: " + what);
}
/// What the new thread runs: the function, then it says it is done.
Run()
{
number = (i32)CurrentNative();
try
{
body();
}
finally
{
done.Store(1);
}
}
public:
new(func body)
{
this.body = body;
done = new AtomicI32();
}
/// Starts the thread (once).
Start()
{
if (state != 0)
Misused("it was started already");
state = 1;
handle = StartNative(() => { Run(); });
}
/// Waits until the thread has finished.
Join()
{
if (state == 0)
Misused("it was not started");
if (state == 1)
{
JoinNative(handle, -1);
handle = 0;
state = 2;
}
}
/// Waits at most `milliseconds`: whether the thread has finished.
Join(i32 milliseconds) : bool
{
if (state == 0)
Misused("it was not started");
if (state == 2)
return true;
if (JoinNative(handle, milliseconds) == 0)
return false;
handle = 0;
state = 2;
return true;
}
/// Sleeps `milliseconds` (other threads run).
static Sleep(i32 milliseconds)
{
SleepFor(milliseconds);
}
/// Lets another thread run.
static Yield()
{
YieldNative();
}
/// Waits a little without giving the processor up (in a loop that waits for another thread).
static SpinWait(i32 iterations)
{
i32 sink = 0;
loop (i32 i = 0..iterations)
sink += i;
}
/// Started and not finished.
bool IsAlive
{
get => state != 0 && done.Load() == 0;
}
/// Its number (0 until it runs): the main thread's is 1.
i32 ManagedThreadId
{
get => number;
}
/// The number of the thread that asks.
static i32 CurrentId
{
get => (i32)CurrentNative();
}
}
}