<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">## MCE::Shared for Perl

This document describes MCE::Shared version 1.003.

### Description

This module provides data sharing capabilities for
[MCE](https://github.com/marioroy/mce-perl) supporting threads and processes.

MCE::Hobo, included with the distribution, provides threads-like parallelization
for running code asynchronously. Unlike threads, Hobo workers are spawned as
processes having unique PIDs.

### Synopsis

```perl
 use MCE::Hobo;
 use MCE::Shared;

 my $N   = shift || 4_000_000;
 my $pi  = MCE::Shared-&gt;scalar( 0.0 );

 my $seq = MCE::Shared-&gt;sequence(
    { chunk_size =&gt; 200_000, bounds_only =&gt; 1 },
    0, $N - 1
 );

 sub compute_pi {
    my ( $wid ) = @_;

    while ( my ( $beg, $end ) = $seq-&gt;next ) {
       my ( $_pi, $t ) = ( 0.0 );
       for my $i ( $beg .. $end ) {
          $t = ( $i + 0.5 ) / $N;
          $_pi += 4.0 / ( 1.0 + $t * $t );
       }
       $pi-&gt;incrby( $_pi );
    }

    return;
 }

 MCE::Hobo-&gt;create( \&amp;compute_pi, $_ ) for ( 1 .. 8 );

 # ... do other stuff ...

 $_-&gt;join() for MCE::Hobo-&gt;list();

 printf "pi = %0.13f\n", $pi-&gt;get / $N;  # 3.1415926535898
```

The next demonstration does the same thing using a MCE Model.

```perl
 use MCE::Flow;
 use MCE::Shared;

 my $N  = shift || 4_000_000;
 my $pi = MCE::Shared-&gt;scalar( 0.0 );

 sub compute_pi {
    my ( $wid, $beg_seq, $end_seq ) = @_;
    my ( $_pi, $t ) = ( 0.0 );

    foreach my $i ( $beg_seq .. $end_seq ) {
       $t = ( $i + 0.5 ) / $N;
       $_pi += 4.0 / ( 1.0 + $t * $t );
    }

    $pi-&gt;incrby( $_pi );
 }

 # Compute bounds only, workers receive [ begin, end ] values

 MCE::Flow::init(
    chunk_size  =&gt; 200_000,
    max_workers =&gt; 8,
    bounds_only =&gt; 1
 );

 mce_flow_s sub {
    compute_pi( MCE-&gt;wid, $_-&gt;[0], $_-&gt;[1] );
 }, 0, $N - 1;

 printf "pi = %0.13f\n", $pi-&gt;get / $N;  # 3.1415926535898
```

### Installation and Dependencies

To install this module type the following:

    perl Makefile.PL

    make
    make test
    make install

This module requires Perl 5.10.1 or later to run.

MCE::Shared utilizes the following modules:

    bytes
    constant
    overload
    Carp
    IO::FDPass (optional)
    MCE::Mutex 1.702+
    MCE::Util 1.702+
    POSIX
    Scalar::Util
    Socket
    Storable 2.04+
    Symbol
    Test::More 0.45+ (for make test only)
    Time::HiRes

The IO::FDPass module is used by MCE::Shared::Server if available and applies
to MCE::Shared::{ Condvar, Handle, and Queue }.

### Further Reading

The MCE::Shared module is described at https://metacpan.org/pod/MCE::Shared.

The MCE::Hobo module is described at https://metacpan.org/pod/MCE::Hobo.

See [MCE::Examples](https://metacpan.org/pod/MCE::Examples)
and [MCE Cookbook](https://github.com/marioroy/mce-cookbook) for recipes.

### Copyright and Licensing

Copyright (C) 2016 by Mario E. Roy &lt;marioeroy AT gmail DOT com&gt;

This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself:

        a) the GNU General Public License as published by the Free
        Software Foundation; either version 1, or (at your option) any
        later version, or

        b) the "Artistic License" which comes with this Kit.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
the GNU General Public License or the Artistic License for more details.

You should have received a copy of the Artistic License with this
Kit, in the file named "LICENSE".  If not, I'll be glad to provide one.

You should also have received a copy of the GNU General Public License
along with this program in the file named "Copying". If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA or visit their web page on the internet at
http://www.gnu.org/copyleft/gpl.html.

</pre></body></html>