Implement Scoped Locks for ICom
There is a problem with the current locking mechanism in the ICom
class. If an exception is thrown in the middle of the lock/unlock
sequence, then the unlock
is never called.
If the exception is not caught, the process crashes and all file handles / locks are released. Thus there is no problem.
If the exception is caught and handled in a way such that the process continues running, then the skipped unlock causes the current process maintaining a lock forever. This blocks other processes from accessing the device.
A proposed solution is to use scoped locks, potentially via std::scoped_lock. This causes the "counter" to be automatically decremented once a function completes without an explicit call to unlock.