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

async can't modify out-of-scope simple variables #7813

Closed
staticfloat opened this issue Aug 1, 2014 · 3 comments
Closed

async can't modify out-of-scope simple variables #7813

staticfloat opened this issue Aug 1, 2014 · 3 comments

Comments

@staticfloat
Copy link
Sponsor Member

Let us imagine we have a simple variable, such as a number. We call an @async block that uses it and modifies it, but are then surprised that the value is not reflected in the outer scope. Example:

x = 0
@sync begin
    x += 10
    @async begin
        x += 20
    end
end
println("Unboxed: $x")

x = [0]
@sync begin
    x[1] += 10
    @async begin
        x[1] += 20
    end
end
println("Boxed: $x")

Result:

$ julia blah.jl 
Unboxed: 10
Boxed: [30]

Is there any way this can be fixed? I feel like this has something to do with the fact that x is immutable object.

@JeffBezanson
Copy link
Sponsor Member

All these constructs copy bindings, since they were designed for distributed memory where another processor's variables cannot (efficiently), and should not be updated this way.

Use @task directly, or a version of @async (found in task.jl) that does not call localize_vars.

closing due to many related issues: #2669 #5930 #3713 #6760

@staticfloat
Copy link
Sponsor Member Author

Great, that fixed my problem, Jeff!

For those looking at this in the future, I was able to get something that "just works" by changing my @async calls to @task, saving the resultant Task object and passing it to Base.sync_add() and Base.enq_work():

x = 0
@sync begin
    x += 10
    t = @task begin
        x += 20
    end
    Base.sync_add(t)
    Base.enq_work(t)
end

println("Unboxed: $x")

@JeffBezanson
Copy link
Sponsor Member

I think we should probably change @async not to call localize_vars, since it doesn't do anything with remote memory. However the other thing localize_vars does is copy in global variables, which is kind of useful.

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

No branches or pull requests

2 participants