gi-glib-2.0.24: GLib bindings

Copyright | Will Thompson Iñaki García Etxebarria and Jonas Platte
License | LGPL-2.1
Maintainer | Iñaki García Etxebarria
Safe Haskell | None
Language | Haskell2010

GI.GLib.Structs.Thread

Contents

- Exported types
- Methods
  - Overloaded methods
  - errorQuark
  - exit
  - join
  - new
  - ref
  - self
  - tryNew
  - unref
  - yield

Description

The `Thread` struct represents a running thread. This struct is returned by `threadNew` or `threadTryNew`. You can obtain the `Thread` struct representing the current thread by calling `threadSelf`.

GThread is refcounted, see `threadRef` and `threadUnref`. The thread represented by it holds a reference while it is running, and `threadJoin` 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.

Synopsis

- newtype Thread = Thread (ManagedPtr Thread)
- threadErrorQuark :: (HasCallStack, MonadIO m) => m Word32
- threadExit :: (HasCallStack, MonadIO m) => Ptr () -> m ()
- threadJoin :: (HasCallStack, MonadIO m) => Thread -> m (Ptr ())
- threadNew :: (HasCallStack, MonadIO m) => Maybe Text -> ThreadFunc -> m Thread
- threadRef :: (HasCallStack, MonadIO m) => Thread -> m Thread
- threadSelf :: (HasCallStack, MonadIO m) => m Thread
- threadTryNew :: (HasCallStack, MonadIO m) => Maybe Text -> ThreadFunc -> m Thread
- threadUnref :: (HasCallStack, MonadIO m) => Thread -> m ()
- threadYield :: (HasCallStack, MonadIO m) => m ()

# Exported types

newtype Thread Source #

Memory-managed wrapper type.

Constructors

Thread (ManagedPtr Thread) |

#### Instances

Instances details

Eq Thread Source # |
Instance details Defined in GI.GLib.Structs.Thread Methods (==) :: Thread -> Thread -> Bool # (/=) :: Thread -> Thread -> Bool #
IsGValue Thread Source # | Convert `Thread` to and from `GValue` with `toGValue` and `fromGValue`.
Instance details Defined in GI.GLib.Structs.Thread Methods toGValue :: Thread -> IO GValue Source # fromGValue :: GValue -> IO Thread Source #
ManagedPtrNewtype Thread Source # |
Instance details Defined in GI.GLib.Structs.Thread Methods toManagedPtr :: Thread -> ManagedPtr Thread Source #
TypedObject Thread Source # |
Instance details Defined in GI.GLib.Structs.Thread Methods glibType :: IO GType Source #
GBoxed Thread Source # |
Instance details Defined in GI.GLib.Structs.Thread
HasParentTypes Thread Source # |
Instance details Defined in GI.GLib.Structs.Thread
type ParentTypes Thread Source # |
Instance details Defined in GI.GLib.Structs.Thread type ParentTypes Thread = '[] :: [Type]

# Methods

## Overloaded methods

## errorQuark

threadErrorQuark :: (HasCallStack, MonadIO m) => m Word32 Source #

No description available in the introspection data.

## exit

threadExit Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Ptr () | `retval`: the return value of this thread
-> m () |

Terminates the current thread.

If another thread is waiting for us using `threadJoin` then the waiting thread will be woken up and get `retval` as the return value of `threadJoin`.

Calling `threadExit` with a parameter `retval` is equivalent to returning `retval` from the function `func`, as given to `threadNew`.

You must only call `threadExit` from a thread that you created yourself with `threadNew` or related APIs. You must not call this function from a thread created with another threading library or or from within a `ThreadPool`.

## join

threadJoin Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Thread | `thread`: a `Thread`
-> m (Ptr ()) | Returns: the return value of the thread

Waits until `thread` finishes, i.e. the function `func`, as given to `threadNew`, returns or `threadExit` is called. If `thread` has already terminated, then `threadJoin` returns immediately.

Any thread can wait for any other thread by calling `threadJoin`, not just its 'creator'. Calling `threadJoin` from multiple threads for the same `thread` leads to undefined behaviour.

The value returned by `func` or given to `threadExit` is returned by this function.

`threadJoin` consumes the reference to the passed-in `thread`. This will usually cause the `Thread` struct and associated resources to be freed. Use `threadRef` to obtain an extra reference if you want to keep the GThread alive beyond the `threadJoin` call.

## new

threadNew Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Maybe Text | `name`: an (optional) name for the new thread
-> ThreadFunc | `func`: a function to execute in the new thread
-> m Thread | Returns: the new `Thread`

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 `threadExit` is called from the new thread. The return value of `func` becomes the return value of the thread, which can be obtained with `threadJoin`.

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 `threadTryNew` if you want to attempt to deal with failures.

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

To free the struct returned by this function, use `threadUnref`. Note that `threadJoin` implicitly unrefs the `Thread` 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.

Since: 2.32

## ref

threadRef Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Thread | `thread`: a `Thread`
-> m Thread | Returns: a new reference to `thread`

Increase the reference count on `thread`.

Since: 2.32

## self

threadSelf Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> m Thread | Returns: the `Thread` representing the current thread

This function returns the `Thread` corresponding to the current thread. Note that this function does not increase the reference count of the returned struct.

This function will return a `Thread` even 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 as `threadJoin`) on these threads.

## tryNew

threadTryNew Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Maybe Text | `name`: an (optional) name for the new thread
-> ThreadFunc | `func`: a function to execute in the new thread
-> m Thread | Returns: the new `Thread`, or `Nothing` if an error occurred (Can throw `GError`)

This function is the same as `threadNew` except that it allows for the possibility of failure.

If a thread can not be created (due to resource limits), `error` is set and `Nothing` is returned.

Since: 2.32

## unref

threadUnref Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Thread | `thread`: a `Thread`
-> m () |

Decrease the reference count on `thread`, possibly freeing all resources associated with it.

Note that each thread holds a reference to its `Thread` while it is running, so it is safe to drop your own reference to it if you don't need it anymore.

Since: 2.32

## yield

threadYield :: (HasCallStack, MonadIO m) => m () Source #

Causes the calling thread to voluntarily relinquish the CPU, so that other threads can run.

This function is often used as a method to make busy wait less evil.