Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^6: Why should any one use/learn Perl 6?

by marioroy (Prior)
on Jun 13, 2018 at 11:27 UTC ( [id://1216547]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Why should any one use/learn Perl 6?
in thread Why should any one use/learn Perl 6?

Greetings liz, MCE::Shared provides two interfaces OO and TIE. The OO interface does not involve TIE.

  • Comment on Re^6: Why should any one use/learn Perl 6?

Replies are listed 'Best First'.
Re^7: Why should any one use/learn Perl 6?
by liz (Monsignor) on Jun 13, 2018 at 18:10 UTC

    Greetings marioroy.

    Indeed, I stand corrected. However, I think that 1nickt's point about it not doing any locking and solve the issue that Perl 6 has when updating a container from multiple threads at the same time, is not true. From the MCE::Shared documentation:

    # Locking is necessary when multiple workers update the same # element. The reason is that it may involve 2 trips to the # shared-manager process: fetch and store in this case. $mutex->enter( sub { $cnt += 1 } );

    This implies to me that MCE::Shared suffers from exactly the same issues that Perl 6 has and which Jonathan so aptly describes in blog post. Or am I missing something?

      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

        Thank you for your elaboration.

        If I understand you correctly, you either have a little boilerplate with tie

        tie my $var, 'MCE::Shared', 0; $var = 42;

        or you have a lot of boilerplate without tie

        my $var = MCE::Shared->share( { module => 'My::Scalar' }, 0 ); $var->set(42)

        And underneath you have locking going on in either case. Is that a correct summary?

        Please note that I really like what you have done with MCE!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216547]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-19 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found