如何在Julia中使用锁 [英] how to use lock in 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中的synchronized
.
It's like synchronized
in Java.
朱莉娅中的synchronized
是什么?
推荐答案
There is also a @sync
macro:
帮助?> @sync
help?> @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屋!