3 ms·
So what? If you drop the lock guard, you get a straightforward compiler error if you later try to access the data in it. You don't get a runtime bug that you
by kelnos 2y ago
So what? If you drop the lock guard, you get a straightforward compiler error if you later try to access the data in it. You don't get a runtime bug that you have to track down like you get if you forget a pthread_mutex_unlock().
- dathinab 2y agoyes that is the point you can rely on implicit drop to always make sure you never forget to unlock (as long as you don't leak the guard which is hard to do but possible) but you can also explicitly drop it to have full control over when exactly it will unlock and in neither situation you have to worry about accidentally bypassing it and in complicated code relying implicitly on a mutex or similar to still be kept it's a common pattern to do so (i.e. to explicitly drop the mutex guard even if not needed to be extra clear about for how long exactly the lock is held)