Convenience library: Monitor (synchronization)


#1

Hello everyone!

I have released Monitor - a convenience library that provides an easier way to use the combination of Mutex+Condvar. This construct is also known as Java’s synchronized() statement.

Suggestions/bug reports/contributions welcome.


#2

Interesting, it could use some documentation though. Why is guard wrapped in an Option? Also, what about multiple condition variables?


#3

Maybe I can improve it based on your questions.

That is an implementation thing. The reason it’s wrapped in an option is because Condvar::wait*() methods take the MutexGuard by value. Do you think it’s worth fighting the type checker to maybe get a very slight performance improvement?

Interesting question. I don’t really know how that fits in with the paradigm. Do you have a specific example in mind? In any case, based on my experience with Java (where a monitor only supports a single condvar) I can say that a single-condvar Monitor is a useful construct.


#4

Documenting the module and the pub items with /// an //! comments is good practice. I wasn’t thinking about performance, but removing the Option or at least the unwraps would be an assurance that the implementation is sound and won’t panic. I don’t really have an use case, it seems that a single condvar is the most common.