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 Threads.@once #51716

Open
chrstphrbrns opened this issue Oct 15, 2023 · 5 comments
Open

add Threads.@once #51716

chrstphrbrns opened this issue Oct 15, 2023 · 5 comments
Labels
good first issue Indicates a good issue for first-time contributors to Julia multithreading Base.Threads and related functionality

Comments

@chrstphrbrns
Copy link
Contributor

It might be useful to have a Threads.@once macro that guarantees a line of code will only be executed once, either globally or in a given scope, a la C++ call_once or GCD dispatch_once

@nsajko
Copy link
Contributor

nsajko commented Oct 15, 2023

xref C++, Go

@Seelengrab
Copy link
Contributor

Seelengrab commented Oct 16, 2023

So something like a decrement-only semaphore that other tasks aren't blocked by if they can't decrement, guarding the function call?

@nsajko
Copy link
Contributor

nsajko commented Oct 16, 2023

@Seelengrab
Copy link
Contributor

Seelengrab commented Oct 16, 2023

Yes, you can use a lock and trylock as well. It would look like this:

l = Spinlock()
# ...
if trylock(l)
    # call f
end

You'd have to keep the done-ness around of course, which is where a "decrement only" semaphore saves some space, by having a smaller struct.

@mbauman mbauman changed the title @once add Threads.@once Oct 16, 2023
@brenhinkeller brenhinkeller added the multithreading Base.Threads and related functionality label Oct 17, 2023
@adienes
Copy link
Member

adienes commented Jan 29, 2025

I think this was implemented in #55793

I guess we can leave the issue open if someone feels like also adding a macro to wrap those functions and use the feature more easily?

@adienes adienes added the good first issue Indicates a good issue for first-time contributors to Julia label Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Indicates a good issue for first-time contributors to Julia multithreading Base.Threads and related functionality
Projects
None yet
Development

No branches or pull requests

5 participants