如何在 Julia 中使用锁 [英] how to use lock in Julia
问题描述
我正在与 Julia 合作.IDE 是 Juno.
I'm working with Julia. The IDE is Juno.
如果我是对的,@async
可以生成一个任务,它就像一个线程.
所以我们可以这样做:
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
现在,我需要锁定一个线程.例如,do something1
是将消息推送到列表,do something2
是从同一列表中弹出消息.
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.
这就像 Java 中的 同步
.
It's like synchronized
in Java.
Julia中的同步
是什么?
what is synchronized
in julia?
推荐答案
还有一个@sync
宏:
There is also a @sync
macro:
帮助?> @sync
等到@async、@spawn、@spawnat 的所有动态封闭使用和@parallel 是完整的.封闭的异步引发的所有异常操作被收集并作为 CompositeException 抛出.
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
这篇关于如何在 Julia 中使用锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!