I need to check whether a file is locked using boost::interprocess::file_lock
. I produced this, but I'm worried what it's gonna do:
bool DataCache::isLocked() const {
bool res = lock_->try_lock();
if(res)
lock_->unlock();
return res;
}
Is it a good idea? Isn't there a way to check it without locking it?
As this doesn't fit in a comment: You could create "interface functions" for tryLock
and unlock
externally.
e.g.:
bool DataCache::try_lock() const {
return lock_->try_lock();
}
void DataCache::unlock() const {
lock_->unlock();
}
Usage:
DataCache cache;
if(cache.try_lock())
{
cache.doSomething();
cache.unlock();
}
else
{
//....
}
I'm not sure if the const
will work here. I just copied it from the question code.