NAME

TB2::CanThread - A role for an object which must be aware of threading

SYNOPSIS

package MyThing;

use TB2::Mouse;
with 'TB2::CanThread';

# Don't coordinate with other threads
my $uncoordinated = MyThing->new(
    coordinate_threads => 0
);

# Coordinate with other threads by default
my $coordinated = MyThing->new;

# If coordinate_threads are on and threads are loaded, these will
# do their thing.  Otherwise they are no-ops.
$object = $coordinated->shared_clone($object);
$coordianted->lock($thing);
$coordianted->share(\@array);

DESCRIPTION

This role manages the sharing of objects between threads.

METHODS

Attributes

coordinate_threads

If true, this TestState will coordinate its events across threads.

If false, events in child threads will not be seen by other threads. Each thread will have a detached state.

Default is true, to coordinate.

This cannot be changed once the TestState has been constructed.

threads::shared methods

These all work like their threads::shared counterparts if and only if $obj->coordinate_threads is true. Otherwise they are no-ops.

share

$obj->share(\$simple_variable);

shared_clone

my $clone = $obj->shared_clone($deep_variable);

If $obj->coordinate_threads is false, this will simply return the $variable.

lock

lock($var) if $obj->coordinate_threads;

Use the normal Perl lock() function if and only if <$obj-coordinate_threads>> is true.

Unfortunately, we cannot provide a lock() method to do this for you. The scope of the lock is lock()'s lexical.

SEE ALSO

threads::shared