spawn
Spawns a new thread which executes function using args and returns a handle to the spawned thread.
config can be used as hints to the platform for how to spawn and execute the function.
The caller must eventually either call join() to wait for the thread to finish and free its resources
or call detach() to excuse the caller from calling join() and have the thread clean up its resources on completion.
Function parameters
Parameters
- function:anytype
- args:anytype
Type definitions in this namespace
Types
- SpawnConfig
- Configuration options for hints on how to spawn threads.
Spurious wakeups are possible and no precision of timing is guaranteed.
Functions
- sleep
- Spurious wakeups are possible and no precision of timing is guaranteed.
- getName
- On Windows, the result is encoded as [WTF-8](https://simonsapin.github.io/wtf-8/).
- getCurrentId
- Returns the platform ID of the callers thread.
- getCpuCount
- Returns the platforms view on the number of logical CPU cores available.
- spawn
- Spawns a new thread which executes `function` using `args` and returns a handle to the spawned thread.
- getHandle
- Returns the handle of this thread
- detach
- Release the obligation of the caller to call `join()` and have the thread clean up its own resources on completion.
- join
- Waits for the thread to complete, then deallocates any resources created on `spawn()`.
- yield
- Yields the current thread potentially allowing other threads to run.
Error sets in this namespace
Error Sets
= native_os != .windows and native_os != .wasi and builtin.link_libc
Values
- use_pthreads
- = native_os != .windows and native_os != .wasi and builtin.link_libc
- max_name_len
- = switch (native_os) { .linux => 15, .windows => 31, .macos, .ios, .watchos, .tvos, .visionos => 63, .netbsd => 31, .freebsd => 15, .openbsd => 23, .dragonfly => 1023, .solaris, .illumos => 31, // https://github.com/SerenityOS/serenity/blob/6b4c300353da49d3508b5442cf61da70bd04d757/Kernel/Tasks/Thread.h#L102 .serenity => 63, else => 0, }
- Handle
- Represents a kernel thread handle.
Source
Implementation
pub fn spawn(config: SpawnConfig, comptime function: anytype, args: anytype) SpawnError!Thread {
if (builtin.single_threaded) {
@compileError("Cannot spawn thread when building in single-threaded mode");
}
const impl = try Impl.spawn(config, function, args);
return Thread{ .impl = impl };
}