4.19. Thread::AutoGate Class

Note: This class is not available with the PO_NO_THREAD_CLASSES parse option.

AutoGate objects, when used along with a Gate object, allow Qore programmers to safely enter and exit a gate lock, even if exceptions are thrown or return statements are executed in the block where the AutoGate object is created. AutoGate objects enter the gate lock for the lifetime of the AutoGate object. For this reason, it is only appropriate to assign an AutoGate object to a local variable, so when the local variable goes out of scope, the AutoGate object will be deleted and the gate automatically exited.

For example:

our Gate $gate();

sub check_error($error) {
    # note that the Gate is entered in the AutoGate constructor, and
    # the Gate will be exited as soon as the block is exited below.
    # (with either the throw statement or the return statement)
    my AutoGate $ag($gate);
    if ($error)
        throw "ERROR", "sorry, an error happened";

    return "OK";
}

Table 4.720. AutoGate Method Overview

Method

Except?

Description

AutoGate::constructor(Gate $gate)

Y

Creates the AutoGate object based on the Gate argument passed and immediately calls Gate::enter().

AutoGate::destructor()

Y

Calls Gate::exit() and destroys the AutoGate object.

AutoGate::copy()

Y

Throws an exception; objects of this class cannot be copied.


4.19.1. AutoGate::constructor()

Synopsis

Creates the AutoGate object based on the Gate argument passed and immediately calls Gate::enter().

Prototype

AutoGate::constructor(Gate $gate)

Example
my AutoGate $gate($gate);

Table 4.721. Arguments for AutoGate::constructor()

Argument

Description

Gate $gate

The Gate object to enter for the lifetime of the AutoGate object.


4.19.2. AutoGate::destructor()

Synopsis

Calls Gate::exit() on the saved Gate object and destroys the AutoGate object.

Example
delete $ag;

4.19.3. AutoGate::copy()

Synopsis

Throws an exception; objects of this class cannot be copied.

Table 4.722. Exceptions Thrown by AutoGate::copy()

err

desc

AUTOGATE-COPY-ERROR

Objects of this class cannot be copied.