1.28 <GThread>

Structure: <GThread>

The <GThread> struct represents a running thread. This struct is returned by thread:new or thread:try-new. You can obtain the <GThread> struct representing the current thread by calling thread-self.

GThread is refcounted, see thread:ref and thread:unref. The thread represented by it holds a reference while it is running, and 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.

Procedures

Procedure: thread:error-quark ⇒ %return
%return

Returned: an unsigned 32-bit integer.

Procedure: thread:exit [retval]

Terminates the current thread.

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

Calling thread-exit with a parameter retval is equivalent to returning retval from the function func, as given to thread:new.

You must only call thread-exit from a thread that you created yourself with thread:new or related APIs. You must not call this function from a thread created with another threading library or or from within a GThreadPool.

retval

Passed, or left out: a foreign pointer, or a bytevector, or #f.

the return value of this thread

Method: thread:get-name self ⇒ %return
Method: get-name self ⇒ %return

Gets the name of the thread.

This function is intended for debugging purposes.

self

Passed: a <GThread>.

a thread

%return

Returned: a string.

the name of the thread

Method: thread:join self ⇒ %return
Method: join self ⇒ %return

Waits until self finishes, i.e. the function func, as given to thread:new, returns or thread-exit is called. If self has already terminated, then thread:join returns immediately.

Any thread can wait for any other thread by calling thread:join, not just its ’creator’. Calling thread:join from multiple threads for the same self leads to undefined behaviour.

The value returned by func or given to thread-exit is returned by this function.

thread:join consumes the reference to the passed-in self. This will usually cause the <GThread> struct and associated resources to be freed. Use thread:ref to obtain an extra reference if you want to keep the GThread alive beyond the thread:join call.

self

Passed: a <GThread>.

a <GThread>

%return

Returned: a foreign pointer, or #f.

the return value of the thread

Procedure: thread:new name func ⇒ %return

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 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 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 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 thread:unref. Note that 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.

name

Passed: a string, or #f.

an (optional) name for the new thread

func

Passed: a procedure.

a function to execute in the new thread

See GThreadFunc for how it is called.

%return

Returned: a <GThread>.

the new <GThread>

data

Not passed: guile-gi supplies it.

an argument to supply to the new thread

Method: thread:ref self ⇒ %return
Method: ref self ⇒ %return

Increase the reference count on self.

self

Passed: a <GThread>.

a <GThread>

%return

Returned: a <GThread>.

a new reference to self

Procedure: thread:self ⇒ %return

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

This function will return a <GThread> 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 thread:join) on these threads.

%return

Returned: a <GThread>.

the <GThread> representing the current thread

Procedure: thread:try-new name func ⇒ %return

This function is the same as 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 #f is returned.

name

Passed: a string, or #f.

an (optional) name for the new thread

func

Passed: a procedure.

a function to execute in the new thread

See GThreadFunc for how it is called.

%return

Returned: a <GThread>.

the new <GThread>, or #f if an error occurred

data

Not passed: guile-gi supplies it.

an argument to supply to the new thread

Method: thread:unref self
Method: unref self

Decrease the reference count on self, 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.

self

Passed: a <GThread>.

a <GThread>

Procedure: thread:yield

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.