Parallelism: Should wait(::Condition)
take a Mutex
to unlock/lock?
#30026
Labels
multithreading
Base.Threads and related functionality
I saw that Base provides a
wait(::Condition)
function, to add yourself to theCondition
's queue:https://docs.julialang.org/en/v1/stdlib/Distributed/#Base.wait
julia/base/event.jl
Lines 40 to 51 in d789231
Unless I'm missing some other mechanism that exists for this, I think we also need a way to provide the
Condition
with aMutex
that it will unlock before sleeping, and re-lock upon awaking.After the multithreading support is in and tasks can run in parallel, I think this is necessary in order to use
Condition
s for synchronization.The pattern I'm describing is the Mesa Monitor, and the wikipedia article covers the required API nicely:
https://en.wikipedia.org/wiki/Monitor_(synchronization)#Condition_variables_2
We can either have the
wait
function take amutex
argument, as in the above Wikipedia example, or we could provide the mutex in theCondition
constructor.C++ takes the first approach:
void wait( std::unique_lock<std::mutex>& lock )
https://en.cppreference.com/w/cpp/thread/condition_variable/wait
And Golang takes the second approach:
func NewCond(l Locker) *Cond
https://golang.org/pkg/sync/#Cond.Wait
https://golang.org/pkg/sync/#NewCond
I'm not totally sure about the tradeoffs between those two, but I guess seeing as most of our Concurrency API is based on Go's, it might be good take the decision they made.
Here is an example of using the pattern I'm describing:
I'm happy to open a PR for this as well, but I just wanted to check that I'm not missing something obvious before doing so! :)
The text was updated successfully, but these errors were encountered: