Yes, they can be used together. It should be perfectly fine to take a lock in an async context and do a relatively quick operation with the guarded state (assuming it's relatively uncontended and other threads aren't holding the lock for long periods of time). There is unfortunately no one best definition of what you should and shouldn't do while holding a lock and when it's best to use one in both sync and async contexts. It's up to developer to profile and determine for themselves how and when to use these kinds of primitives in their code bases.
Yes! This mutex is stored inline and does not require an extra allocation that older types had to do. On Darwin and Windows platforms that's mostly the biggest advantage this type has because they are just calling into system implementations to lock and unlock. For Linux, we use the futex
primitives to make a relatively simple mutex type that's really cheap to acquire and release in the uncontended case. This implementation should prove to be at the very least a small win over implementations that use pthread_mutex_t
.