Function

GLibThreadnew

since: 2.32

Declaration

GThread*
g_thread_new (
  const gchar* name,
  GThreadFunc func,
  gpointer data
)

Description

This function creates a new thread. The new thread starts by invoking func with the argument data. The thread will run until func returns or until g_thread_exit() is called from the new thread. The return value of func becomes the return value of the thread, which can be obtained with g_thread_join().

The name can 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 of name to 16 bytes.

If the thread can not be created the program aborts. See g_thread_try_new() if you want to attempt to deal with failures.

If you are using threads to offload (potentially many) short-lived tasks, GThreadPool may be more appropriate than manually spawning and tracking multiple GThreads.

To free the struct returned by this function, use g_thread_unref(). Note that g_thread_join() implicitly unrefs the GThread as 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.

Available since: 2.32

Parameters

name

Type: const gchar*

An (optional) name for the new thread.

The argument can be NULL.
The data is owned by the caller of the function.
The value is a NUL terminated UTF-8 string.
func

Type: GThreadFunc

A function to execute in the new thread.

data

Type: gpointer

An argument to supply to the new thread.

The argument can be NULL.
The data is owned by the caller of the function.

Return value

Type: GThread

The new GThread.

The caller of the function takes ownership of the data, and is responsible for freeing it.