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

don't require Sync trait for job function #1

Merged
merged 1 commit into from
Dec 10, 2022
Merged

don't require Sync trait for job function #1

merged 1 commit into from
Dec 10, 2022

Conversation

mike1703
Copy link

@mike1703 mike1703 commented Dec 4, 2022

What is the reason for the Sync trait for the function a job executes?
I understand the need for Send, but I don't exactly understand why I need Sync here.

Sync would be necessary if I want to share data between e.g. threads. But this is not really necessary from my perspective.
I want to be able to use std::sync::mpsc to regularly send some commands in a Sender.
Sender implements Send but no Sync. To use a Sender from within the function called from the job I would need to move the necessary variables inside the closure.

example code:

let command_tx: Sender<String> = ...;
scheduler.add(Job::new("1/10 * * * * *".parse().unwrap(), {
    move || {
        command_tx
            .send("I get executed every 10 seconds!".into())
            .unwrap();
    }
}));

without my commit this produces the following error:

`std::sync::mpsc::Sender<std::string::String>` cannot be shared between threads safely

I might overlook some important part here, but for my example code this worked out fine.

BlackDex added a commit that referenced this pull request Dec 10, 2022
 - Don't require Sync trait for job function (PR #1 - Thanks @mikel1703)
 - Added two other examples. One using threading, and one also using MPSC.
 - Added some clippy checks
 - Fixed some spelling errors
@BlackDex BlackDex merged commit cd6f1ce into BlackDex:master Dec 10, 2022
@BlackDex
Copy link
Owner

Thanks @mike1703. You are right, Sync is not needed at all.
I also have created an example using MPSC. That should help for others and future updates to not have any issues with your use case.

@mike1703
Copy link
Author

Thank you for merging! :)

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.

2 participants