THR_NEW(2)
okay so bestie, welcome to the THR_NEW(2) page from the System Calls Manual β this is all about thr_new, and we're gonna make it make sense π
β¨
β¨ NAME β¨
thr_newβ create new thread of execution (yes, a whole new thread, slay π§΅π)
π LIBRARY π
Standard C Library (libc, -lc) β she lives in libc, bestie π«Ά
π¦ SYNOPSIS π¦
#include <sys/thr.h>
int
thr_new(struct thr_param *param, int param_size);
π DESCRIPTION π
okay so the tea is: this function is intended for implementing threading. Normal applications should call pthread_create(3) instead. Like, unless you're building the threading machinery itself, this is not your everyday hangout spot, bestie π
The thr_new() system call creates a new kernel-scheduled thread of execution in the context of the current process. The newly created thread shares all attributes of the process with the existing kernel-scheduled threads in the process, but has private processor execution state. The machine context for the new thread is copied from the creating thread's context, including coprocessor state. FPU state and specific machine registers are excluded from the copy. These are set according to ABI requirements and syscall parameters. The FPU state for the new thread is reinitialized to clean. so fresh, so clean β¨
The param structure supplies parameters affecting the thread creation. The structure is defined in the <sys/thr.h> header as follows:
struct thr_param {
void (*start_func)(void *);
void *arg;
char *stack_base;
size_t stack_size;
char *tls_base;
size_t tls_size;
long *child_tid;
long *parent_tid;
int flags;
struct rtprio *rtp;
};
and it contains the following fields:
start_funcβ Pointer to the thread entry function. The kernel arranges for the new thread to start executing the function upon the first return to userspace. first-return-to-userspace realness πargβ Opaque argument supplied to the entry function.stack_baseβ Stack base address. The stack must be allocated by the caller. On some architectures, the ABI might require that the system put information on the stack to ensure the execution environment forstart_func.stack_sizeβ Stack size.tls_baseβ TLS base address. The value of TLS base is loaded into the ABI-defined machine register in the new thread context.tls_sizeβ TLS size.child_tidβ Address to store the new thread identifier, for the child's use.parent_tidβ Address to store the new thread identifier, for the parent's use.
Both child_tid and parent_tid are provided, with the intent that child_tid is used by the new thread to get its thread identifier without issuing the thr_self(2) syscall, while parent_tid is used by the thread creator. The latter is separate from child_tid because the new thread might exit and free its thread data before the parent has a chance to execute far enough to access it. timing is literally everything π
flagsβ Thread creation flags. Theflagsmember may specify the following flags:THR_SUSPENDEDβ Create the new thread in the suspended state. The flag is not currently implemented.THR_SYSTEM_SCOPEβ Create the system scope thread. The flag is not currently implemented.THR_C_RUNTIMEβ Indicate that the new thread is created by the C language runtime. It has architecture-specific meaning. On amd64, the flag requests that the specifiedtls_basewas loaded into the%fsbaseregister before calling a signal handler.
rtpβ Real-time scheduling priority for the new thread. May beNULLto inherit the priority from the creating thread.
The param_size argument should be set to the size of the param structure. no cap, just set it right π«Ά
After the first successful creation of an additional thread, the process is marked by the kernel as multi-threaded. In particular, the P_HADTHREADS flag is set in the process' p_flag (visible in the ps(1) output), and several operations are executed in multi-threaded mode. For instance, the execve(2) system call terminates all threads but the calling one on successful execution. iconic but also kind of dramatic π
πΈ RETURN VALUES πΈ
If successful, thr_new() will return zero, otherwise -1 is returned, and errno is set to indicate the error. slay when it works, cry when it doesn't π
π ERRORS π
The thr_new() operation returns the following errors:
[EFAULT]β The memory pointed to by theparamargument is not valid.[EFAULT]β The memory pointed to by theparamstructurechild_tid,parent_tidorrtparguments is not valid.[EFAULT]β The specified stack base is invalid, or the kernel was unable to put required initial data on the stack.[EINVAL]β Theparam_sizeargument specifies a negative value, or the value is greater than the largeststruct paramsize the kernel can interpret.[EINVAL]β Thertpmember is notNULLand specifies invalid scheduling parameters.[EINVAL]β The specified TLS base is invalid.[EPERM]β The caller does not have permission to set the scheduling parameters or scheduling policy.[EPROCLIM]β Creation of the new thread would exceed theRACCT_NTHRlimit, seerctl_get_racct(2).[EPROCLIM]β Creation of the new thread would exceed thekern.threads.max_threads_per_procsysctl(3)limit.[ENOMEM]β There was not enough kernel memory to allocate the new thread structures.
π₯ SEE ALSO π₯
ps(1), _umtx_op(2), execve(2), rctl_get_racct(2), thr_exit(2), thr_kill(2), thr_kill2(2), thr_self(2), thr_set_name(2), pthread_create(3) β go read these too, bestie π«Ά
π STANDARDS π
The thr_new() system call is non-standard and is used by the 1:1 Threading Library (libthr, -lthr) to implement IEEE Std 1003.1-2001 (βPOSIX.1β) pthread(3) functionality. so she's the lowkey engine behind your pthreads, iykyk π
β HISTORY β
The thr_new() system call first appeared in FreeBSD 5.2. vintage but still relevant, periodt β¨
π cute little sign-off: this page came from Debian, dated May 5, 2020, and it's the THR_NEW(2) manual page. love u, bye bestie! ππ«Ά
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2016 The FreeBSD Foundation
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms