glib

Struct ThreadPool

Source
pub struct ThreadPool(/* private fields */);
Expand description

The GThreadPool struct represents a thread pool.

A thread pool is useful when you wish to asynchronously fork out the execution of work and continue working in your own thread. If that will happen often, the overhead of starting and destroying a thread each time might be too high. In such cases reusing already started threads seems like a good idea. And it indeed is, but implementing this can be tedious and error-prone.

Therefore GLib provides thread pools for your convenience. An added advantage is, that the threads can be shared between the different subsystems of your program, when they are using GLib.

To create a new thread pool, you use [new()][Self::new()]. It is destroyed by GLib::ThreadPool::free().

If you want to execute a certain task within a thread pool, use push().

To get the current number of running threads you call num_threads(). To get the number of still unprocessed tasks you call unprocessed(). To control the maximum number of threads for a thread pool, you use max_threads(). and set_max_threads().

Finally you can control the number of unused threads, that are kept alive by GLib for future use. The current number can be fetched with num_unused_threads(). The maximum number can be controlled by max_unused_threads() and set_max_unused_threads(). All currently unused threads can be stopped by calling stop_unused_threads().

Implementations§

Source§

impl ThreadPool

Source

pub fn shared(max_threads: Option<u32>) -> Result<Self, Error>

Source

pub fn exclusive(max_threads: u32) -> Result<Self, Error>

Source

pub fn push<T: Send + 'static, F: FnOnce() -> T + Send + 'static>( &self, func: F, ) -> Result<ThreadHandle<T>, Error>

Inserts @data into the list of tasks to be executed by @self.

When the number of currently running threads is lower than the maximal allowed number of threads, a new thread is started (or reused) with the properties given to g_thread_pool_new(). Otherwise, @data stays in the queue until a thread in this pool finishes its previous task and processes @data.

@error can be None to ignore errors, or non-None to report errors. An error can only occur when a new thread couldn’t be created. In that case @data is simply appended to the queue of work to do.

Before version 2.32, this function did not return a success status.

§Returns

true on success, false if an error occurred

Source

pub fn push_future<T: Send + 'static, F: FnOnce() -> T + Send + 'static>( &self, func: F, ) -> Result<impl Future<Output = Result<T>> + Send + Sync + 'static, Error>

Source

pub fn set_max_threads(&self, max_threads: Option<u32>) -> Result<(), Error>

Sets the maximal allowed number of threads for @self. A value of -1 means that the maximal number of threads is unlimited. If @self is an exclusive thread pool, setting the maximal number of threads to -1 is not allowed.

Setting @max_threads to 0 means stopping all work for @self. It is effectively frozen until @max_threads is set to a non-zero value again.

A thread is never terminated while calling @func, as supplied by g_thread_pool_new(). Instead the maximal number of threads only has effect for the allocation of new threads in g_thread_pool_push(). A new thread is allocated, whenever the number of currently running threads in @self is smaller than the maximal number.

@error can be None to ignore errors, or non-None to report errors. An error can only occur when a new thread couldn’t be created.

Before version 2.32, this function did not return a success status.

§max_threads

a new maximal number of threads for @self, or -1 for unlimited

§Returns

true on success, false if an error occurred

Source

pub fn max_threads(&self) -> Option<u32>

Returns the maximal number of threads for @self.

§Returns

the maximal number of threads

Source

pub fn num_threads(&self) -> u32

Returns the number of threads currently running in @self.

§Returns

the number of threads currently running

Source

pub fn unprocessed(&self) -> u32

Returns the number of tasks still unprocessed in @self.

§Returns

the number of unprocessed tasks

Source

pub fn set_max_unused_threads(max_threads: Option<u32>)

Sets the maximal number of unused threads to @max_threads. If @max_threads is -1, no limit is imposed on the number of unused threads.

The default value is 2.

§max_threads

maximal number of unused threads

Source

pub fn max_unused_threads() -> Option<u32>

Returns the maximal allowed number of unused threads.

§Returns

the maximal number of unused threads

Source

pub fn num_unused_threads() -> u32

Returns the number of currently unused threads.

§Returns

the number of currently unused threads

Source

pub fn stop_unused_threads()

Stops all currently unused threads. This does not change the maximal number of unused threads. This function can be used to regularly stop all unused threads e.g. from g_timeout_add().

Source

pub fn set_max_idle_time(max_idle_time: u32)

This function will set the maximum @interval that a thread waiting in the pool for new tasks can be idle for before being stopped. This function is similar to calling g_thread_pool_stop_unused_threads() on a regular timeout, except this is done on a per thread basis.

By setting @interval to 0, idle threads will not be stopped.

The default value is 15000 (15 seconds).

§interval

the maximum @interval (in milliseconds) a thread can be idle

Source

pub fn max_idle_time() -> u32

This function will return the maximum @interval that a thread will wait in the thread pool for new tasks before being stopped.

If this function returns 0, threads waiting in the thread pool for new work are not stopped.

§Returns

the maximum @interval (milliseconds) to wait for new tasks in the thread pool before stopping the thread

Trait Implementations§

Source§

impl Debug for ThreadPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for ThreadPool

Source§

impl Sync for ThreadPool

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.