Basics

Guides

API Reference

Menu

Basics

Guides

API Reference

class: GThread

[20:7] extends: object

The #GThread struct represents a running thread. This struct is returned by g_thread_new() or g_thread_try_new(). You can obtain the #GThread struct representing the current thread by calling g_thread_self(). GThread is refcounted, see g_thread_ref() and g_thread_unref(). The thread represented by it holds a reference while it is running, and g_thread_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.

Members

  • handleObj
  • lib
  • retainedCallbacks
  • signalHandlerNames
  • signalSetterHandlers

Methods

  • GThread (name = null, func = null, data = null)

    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.
    • @p name is an (optional) name for the new thread.
    • @p func is a function to execute in the new thread.
    • @p data is an argument to supply to the new thread.
  • toNativeHandle (Source)

    Normalizes a constructor argument into a raw pointer carrier. Accepts a raw NativeHandle, a raw NativeBuffer returned from fn.call(...), another generated wrapper exposing handle(), or null. Returns null when the argument carries no pointer.

    • @p Source is the raw handle, raw buffer, wrapper, or null.
    • @r A raw pointer carrier or null when no pointer is present.
  • getLib ()

    Returns the opened native library for this generated wrapper.

    • @r The opened native library.
  • handle ()

    Returns the wrapped NativeHandle.

    • @r The wrapped NativeHandle.
  • isNull ()

    Returns true when the wrapped handle is null.

    • @r A bool.
  • describe ()

    Returns a small string for debugging generated wrappers.

    • @r A string.
  • get_name ()

    Gets the name of the thread. This function is intended for debugging purposes.

    • @r the name of the thread.
  • join ()

    Waits until @thread finishes, i.e. the function @func, as given to g_thread_new(), returns or g_thread_exit() is called. If @thread has already terminated, then g_thread_join() returns immediately. Any thread can wait for any other thread by calling g_thread_join(), not just its 'creator'. Calling g_thread_join() from multiple threads for the same

    • @thread leads to undefined behaviour. The value returned by @func or given to g_thread_exit() is returned by this function. g_thread_join() consumes the reference to the passed-in @thread. This will usually cause the #GThread struct and associated resources to be freed. Use g_thread_ref() to obtain an extra reference if you want to keep the GThread alive beyond the g_thread_join() call.
    • @r the return value of the thread.
  • ref ()

    Increase the reference count on @thread.

    • @r a new reference to @thread.
  • set_priority (string priority)

    This function does nothing.

    • @p priority is ignored.
    • @r None.
  • unref ()

    Decrease the reference count on @thread, possibly freeing all resources associated with it. Note that each thread holds a reference to its #GThread while it is running, so it is safe to drop your own reference to it if you don't need it anymore.

    • @r None.

class: GThreadCtors

[195:14] static extends: object

Alternate constructors for GThread. Usage: GThreadCtors.<name>(...). The primary constructor lives directly on GThread.

Methods

  • newTryNew (string name, object func)

    This function is the same as g_thread_new() except that it allows for the possibility of failure. If a thread can not be created (due to resource limits), @error is set and %NULL is returned.

    • @p name is an (optional) name for the new thread.
    • @p func is a function to execute in the new thread.
    • @p data is an argument to supply to the new thread.
    • @r A new GThread.