Thread#
- class Thread(*args, **kwargs)#
The Thread struct represents a running thread. This struct
is returned by new() or try_new(). You can
obtain the Thread struct representing the current thread by
calling self().
GThread is refcounted, see ref() and unref().
The thread represented by it holds a reference while it is running,
and join() consumes the reference that it is given, so
it is normally not necessary to manage GThread references
explicitly.
The structure is opaque – none of its fields may be directly accessed.
Constructors#
- class Thread
- classmethod new(name: str | None, func: Callable[[Any], Any | None], data: Any = None) Thread#
This function creates a new thread. The new thread starts by invoking
funcwith the argument data. The thread will run untilfuncreturns or untilexit()is called from the new thread. The return value offuncbecomes the return value of the thread, which can be obtained withjoin().The
namecan be useful for discriminating threads in a debugger. It is not used for other purposes and does not have to be unique. Some systems restrict the length ofnameto 16 bytes.If the thread can not be created the program aborts. See
try_new()if you want to attempt to deal with failures.If you are using threads to offload (potentially many) short-lived tasks,
ThreadPoolmay be more appropriate than manually spawning and tracking multipleThread.To free the struct returned by this function, use
unref(). Note thatjoin()implicitly unrefs theThreadas well.New threads by default inherit their scheduler policy (POSIX) or thread priority (Windows) of the thread creating the new thread.
This behaviour changed in GLib 2.64: before threads on Windows were not inheriting the thread priority but were spawned with the default priority. Starting with GLib 2.64 the behaviour is now consistent between Windows and POSIX and all threads inherit their parent thread’s priority.
Added in version 2.32.
- Parameters:
name – an (optional) name for the new thread
func – a function to execute in the new thread
data – an argument to supply to the new thread
- classmethod try_new(name: str | None, func: Callable[[Any], Any | None], data: Any = None) Thread#
This function is the same as
new()except that it allows for the possibility of failure.If a thread can not be created (due to resource limits),
erroris set andNoneis returned.Added in version 2.32.
- Parameters:
name – an (optional) name for the new thread
func – a function to execute in the new thread
data – an argument to supply to the new thread
Methods#
- class Thread
-
- classmethod exit() None#
Terminates the current thread.
If another thread is waiting for us using
join()then the waiting thread will be woken up and getretvalas the return value ofjoin().Calling
exit()with a parameterretvalis equivalent to returningretvalfrom the functionfunc, as given tonew().You must only call
exit()from a thread that you created yourself withnew()or related APIs. You must not call this function from a thread created with another threading library or or from within aThreadPool.
- join() Any | None#
Waits until
threadfinishes, i.e. the functionfunc, as given tonew(), returns orexit()is called. Ifthreadhas already terminated, thenjoin()returns immediately.Any thread can wait for any other thread by calling
join(), not just its ‘creator’. Callingjoin()from multiple threads for the samethreadleads to undefined behaviour.The value returned by
funcor given toexit()is returned by this function.join()consumes the reference to the passed-inthread. This will usually cause theThreadstruct and associated resources to be freed. Useref()to obtain an extra reference if you want to keep the GThread alive beyond thejoin()call.
- classmethod self() Thread#
This function returns the
Threadcorresponding to the current thread. Note that this function does not increase the reference count of the returned struct.This function will return a
Threadeven for threads that were not created by GLib (i.e. those created by other threading APIs). This may be useful for thread identification purposes (i.e. comparisons) but you must not use GLib functions (such asjoin()) on these threads.