Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Greetings liz,

A mutex is necessary whenever the Perl-like behavior, via the TIE interface, involves 2 IPC calls { FETCH and STORE }.

use strict; use warnings; use feature 'say'; use MCE::Hobo; use MCE::Mutex; use MCE::Shared; my $mutex = MCE::Mutex->new(); tie my $var, 'MCE::Shared', 0; sub task { for ( 1 .. 2000 ) { $mutex->enter(sub { $var += 1; # FETCH, STORE $var += 4; # Ditto }); } } MCE::Hobo->create(\&task) for 1 .. 4; MCE::Hobo->waitall; say $var; # 40000

A mutex is not necessary via the OO interface. MCE::Shared::{ Array, Hash, and Scalar } include sugar methods.

use strict; use warnings; use feature 'say'; use MCE::Hobo; use MCE::Shared; my $var = MCE::Shared->scalar(0); sub task { for ( 1 .. 2000 ) { $var->incr; $var->incrby(4); } } MCE::Hobo->create(\&task) for 1 .. 4; MCE::Hobo->waitall; say $var->get; # 40000

MCE::Shared works with threads and other parallel modules. Below, the same thing using threads.

use strict; use warnings; use feature 'say'; use threads; use MCE::Shared; my $var = MCE::Shared->scalar(0); sub task { for ( 1 .. 2000 ) { $var->incr; $var->incrby(4); } } threads->create(\&task) for 1 .. 4; $_->join for threads->list; say $var->get; # 40000

One may also customize the shared class feasibly.

use strict; use warnings; package My::Scalar; use MCE::Shared::Scalar; use base 'MCE::Shared::Scalar'; sub set_if_max { # my ( $self, $value ) = @_; ${ $_[0] } = $_[1] if ( $_[1] > ${ $_[0] } ); 1; } 1; package main; use feature 'say'; use MCE::Shared; my $var = MCE::Shared->share( { module => 'My::Scalar' }, 0 ); $var->set_if_max(42); $var->set_if_max(11); say $var->get; # 42

Regards, Mario


In reply to Re^8: Why should any one use/learn Perl 6? by marioroy
in thread Why should any one use/learn Perl 6? by skooma

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-24 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found