public function lock(bool $exclusive) : void
    {
        if ($this->closed) {
            throw new IoException('The stream is closed.');
        }

        try {
            $result = ErrorCatcher::run(function() use ($exclusive) {
                return flock($this->handle, $exclusive ? LOCK_EX : LOCK_SH);
            });
        } catch (\ErrorException $e) {
            throw new IoException($e->getMessage(), 0, $e);
        }

        if ($result === false) {
            throw new IoException('Failed to obtain a lock.');
        }
    }