Struct glib::ThreadPool
source · pub struct ThreadPool(_);
Expand description
The ThreadPool
struct represents a thread pool. It has three
public read-only members, but the underlying struct is bigger,
so you must not copy this struct.
Implementations§
source§impl ThreadPool
impl ThreadPool
pub fn exclusive(max_threads: u32) -> Result<Self, Error>
sourcepub fn push<F: FnOnce() + Send + 'static>(&self, func: F) -> Result<(), Error>
pub fn push<F: FnOnce() + Send + 'static>(&self, func: F) -> Result<(), 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 [new()
][Self::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
pub fn push_future<T: Send + 'static, F: FnOnce() -> T + Send + 'static>( &self, func: F ) -> Result<impl Future<Output = T> + Send + Sync + 'static, Error>
sourcepub fn set_max_threads(&self, max_threads: Option<u32>) -> Result<(), Error>
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
[new()
][Self::new()]. Instead the maximal number of threads only
has effect for the allocation of new threads in 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
sourcepub fn max_threads(&self) -> Option<u32>
pub fn max_threads(&self) -> Option<u32>
sourcepub fn num_threads(&self) -> u32
pub fn num_threads(&self) -> u32
Returns the number of threads currently running in self
.
Returns
the number of threads currently running
sourcepub fn unprocessed(&self) -> u32
pub fn unprocessed(&self) -> u32
sourcepub fn set_max_unused_threads(max_threads: Option<u32>)
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
sourcepub fn max_unused_threads() -> Option<u32>
pub fn max_unused_threads() -> Option<u32>
sourcepub fn num_unused_threads() -> u32
pub fn num_unused_threads() -> u32
sourcepub fn stop_unused_threads()
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()
.
sourcepub fn set_max_idle_time(max_idle_time: u32)
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
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
sourcepub fn max_idle_time() -> u32
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