metasync
metasync copied to clipboard
Implement SharedMutex
-
SharedMutex
allow us to access shared resource in both ways exclusive and shared. - Multiple threads can access resource for read access at a same time
- Only one threads can access resource for write access at a same time
class SharedMutex {
status: SM_UNLOCKED | SM_READ_LOCK | SM_WRITE_LOCK,
async enterRead(callback), // if no callback passed Promise will be returned
async enterWrite(callback),
leave(),
on('status', (status => {})), // or you can subscribe on status change
}