# AutoDrain reference

The scoped drain in &Cpp; has more structure than in Perl. It consists of the base class AutoDrain and two subclasses: AutoDrainShared and AutoDrainExclusive. They are all defined in

    app/AutoDrain.h

. They are all Stargets, and can be used in only one thread.

The base AutoDrain cannot be created directly but is convenient for keeping a reference to any kind of drain:

    Autoref drain = new AutoDrainShared(app);

The constructor of the subclass determines whether the drain is shared or exclusive, and the rest of the methods are defined in the base class.

It's also possible to use the a direct local variable:

    {
    	AutoDrainShared drain(app);
    	...
    }

Just remember not to mix the metaphors, if you create a local variable, don't try to create the references to it.

The constructors are:

    AutoDrainShared(App *app, bool wait = true);
    AutoDrainShared(TrieadOwner *to, bool wait = true);

Create a shared drain from an App or TrieadOwner. The

    wait

flag determines if the constructor will wait for the drain to complete, otherwise it will return immediately. Well, usually immediately, if there is no other incompatible drain active at the moment. The shared drain requests the draining of all the Trieads, and multiple threads may have their shared drains active at the same time (the release will happen when all these drains become released). A shared drain will wait for all the preceding exclusive drains to be released before it gets created.

    AutoDrainExclusive(TrieadOwner *to, bool wait = true);

Create an exclusive drain from a TrieadOwner. The

    wait

flag makes the constructor wait for the completion of the drain. Only one exclusive drain at a time may be active, from only one thread. An exclusive drain will wait for all the preceding shared and exclusive drains to be released before it gets created.

The common method is:

    void wait();

Wait for the drain to complete. Can be called repeatedly. If more data has been injected into the model through the excluded Triead, will wait for that data to drain.

AutoDrain