how to use lock in Julia
Asked Answered
F

2

6

I'm working with Julia. The IDE is Juno.

If I'm right, @async can generate a task, it's just like a thread.
So we can do this:

@async begin
   # do something1
end
@async begin
   # do something2
end

Now, I need to lock a thread. For example, do something1 is to push message to a list and do something2 is to pop message from the same list.

It's like synchronized in Java.

what is synchronized in julia?

Flog answered 18/11, 2015 at 11:30 Comment(1)
Also see: Reactive.jl (and Interactive.jl)Workroom
K
3

There is also a @sync macro:

help?> @sync

Wait until all dynamically-enclosed uses of @async, @spawn, @spawnat and @parallel are complete. All exceptions thrown by enclosed async operations are collected and thrown as a CompositeException.

@sync @async begin
   # do something1
end

@sync begin
    # some code    
    @async begin
        # do something2
    end
    @async # do something 3
end
Kinney answered 18/11, 2015 at 17:21 Comment(0)
F
4

To keep a block mutex:

mutex = RemoteRef()

@async begin
   put!(mutex, true)
   # do something1
   take!(mutex)
end
@async begin
   put!(mutex, true)
   # do something2
   take!(mutex)
end
Flog answered 18/11, 2015 at 15:50 Comment(0)
K
3

There is also a @sync macro:

help?> @sync

Wait until all dynamically-enclosed uses of @async, @spawn, @spawnat and @parallel are complete. All exceptions thrown by enclosed async operations are collected and thrown as a CompositeException.

@sync @async begin
   # do something1
end

@sync begin
    # some code    
    @async begin
        # do something2
    end
    @async # do something 3
end
Kinney answered 18/11, 2015 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.