The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

IPC::Pleather - Easy to use concurrency primitives inspired by Cilk

VERSION

version 0.01

SYNOPSIS

  use IPC::Pleather;

  sub fib {
    my $n = shift;
    return $n if $n < 2;

    spawn my $x = fib($n - 1);
    spawn my $y = fib($n - 2);

    sync $x;
    sync $y;

    return $x + $y;
  }

DESCRIPTION

C has Cilk, Perl has Pleather.

IPC::Pleather adopts two keywords from Cilk, spawn and sync. spawn signals that the block or expression may be executed concurrently. sync denotes a merge point, waiting until the spawned expression is completely resolved.

KEYWORDS

spawn

Declares a variable whose value may or may not be executed in a forked process. Some care is taken to guarantee a fixed cap on the total number of forks. Recursive calls to spawn via the spawned expression (as in the Fibonacci example in the "SYNOPSIS") are bound by the same maximum.

The forking mechanism is implemented using "fork_call" in AnyEvent::Util, allowing the maximum number of processes to be controlled either by setting $AnyEvent::Util::MAX_FORKS or with the PERL_ANYEVENT_MAX_FORKS environmental variable.

spawn accepts several different expression syntaxes.

  # Block with optional arguments
  spawn my $var = {...} $arg1, $arg2, $arg3;

  # Subroutine call
  spawn my $var = do_stuff($arg1, $arg1 + $arg2, ...);

The latter syntax expands to the former, and all expressions in the argument list are evaluated in the subprocess (or not, as the case may be), rather than in the process from which they were spawned.

sync

Causes the process to block until the specified variable is fully resolved. For spawned variables which executed in a forked process, a condition variable is used to synchronize the result. When executed locally, the result is immediately assigned to the variable and the call to sync is essentially a no-op.

SEE ALSO

The Cilk Project

AUTHOR

Jeff Ober <sysread@fastmail.fm>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Jeff Ober.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.