Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods to provide on spawn and on finish tasks to thread pool builder #54

Merged
merged 2 commits into from
Dec 19, 2023

Conversation

fpervaiz
Copy link
Contributor

Following on from discussion in #51, this PR adds two methods to ThreadPoolBuilder: on_thread_spawn and on_thread_finish. These allow for functions to be provided that are called immediately after each pool thread is spawned and immediately before it finishes.

An example of where these may be useful is for setting core affinity for each thread in the thread pool. This use case inspired the original PR (#51); the implementation in this PR is a completely general interface that also makes this possible.

Copy link
Collaborator

@Lancern Lancern left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Owner

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Most looks good, just one little nitpick.

For consistency, we always use explicit generic instead of "impl Trait in argument position" in spdlog-rs.

/// Provide a function that will be called on each thread of the thread pool
/// immediately after it is spawned. This can, for example, be used to set
/// core affinity for each thread.
pub fn on_thread_spawn(&mut self, f: impl Fn() + Send + Sync + 'static) -> &mut Self {
Copy link
Owner

@SpriteOvO SpriteOvO Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn on_thread_spawn(&mut self, f: impl Fn() + Send + Sync + 'static) -> &mut Self {
pub fn on_thread_spawn<F>(&mut self, f: F) -> &mut Self
where
F: Fn() + Send + Sync + 'static
{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/// Provide a function that will be called on each thread of the thread pool
/// just before the thread finishes.
pub fn on_thread_finish(&mut self, f: impl Fn() + Send + Sync + 'static) -> &mut Self {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub fn on_thread_finish(&mut self, f: impl Fn() + Send + Sync + 'static) -> &mut Self {
pub fn on_thread_finish<F>(&mut self, f: F) -> &mut Self
where
F: Fn() + Send + Sync + 'static
{

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@SpriteOvO SpriteOvO merged commit 0d182e2 into SpriteOvO:main Dec 19, 2023
32 checks passed
@SpriteOvO
Copy link
Owner

Appreciate your work! ^^ PR merged and it will be included in v0.4.0 🎉

@SpriteOvO SpriteOvO added this to the v0.4.0 milestone Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants