Library: Foundation
Package: Threading
Header: Poco/IOLock.h
Description
ScopedIOLock is a RAII wrapper for IOLock that automatically calls leave() when the scope exits. This ensures proper cleanup even if an exception is thrown during the IO operation.
Unlike a regular ScopedLock, enter() may fail if the IOLock is already closed. Always check if the lock was successfully entered before proceeding with the IO operation.
Usage pattern:
int readBytes(void* buffer, int length)
{
ScopedIOLock lock(_ioLock);
if (!lock)
return 0; // already closed
// ... perform IO ...
// leave() called automatically at scope exit
if (lock.isClosed())
return 0; // closed during IO
//(from another thread, explicitly
// or through object destruction)
return bytesRead;
}
Note: isClosed() can be called after the scope exits (after leave()) to check if the lock was closed during the IO operation. This is safe because isClosed() checks the closed flag, not whether the lock is held.
Member Summary
Member Functions: entered, isClosed, operator bool
Constructors
ScopedIOLock
explicit ScopedIOLock(
IOLock & lock
);
Creates the ScopedIOLock and attempts to enter the IOLock. Check operator bool() or entered() to see if entry succeeded.
Destructor
~ScopedIOLock
~ScopedIOLock();
Destroys the ScopedIOLock and calls leave() if enter() succeeded.
Member Functions
entered
bool entered() const;
Returns true if enter() succeeded and IO can proceed.
isClosed
bool isClosed() const;
Returns true if the underlying IOLock is closed. Can be called to check if close was requested during IO.
operator bool
explicit operator bool() const;
Returns true if enter() succeeded and IO can proceed.