#!/usr/bin/env perl use strict; use warnings; use MCE::Hobo; use MCE::Shared; use Time::HiRes qw(time); use feature qw(say); my $number = MCE::Shared->scalar(0); my $start = time; MCE::Hobo->create('task', $_) for 1 .. 4; MCE::Hobo->waitall; printf "duration: %0.3f seconds\n", time - $start; say "number: ", $number->get(); sub task { my ($id) = @_; say "Hobo $id started"; $number->incr() for 1 .. 100000; say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 4 ended Hobo 2 ended Hobo 1 ended Hobo 3 ended duration: 0.872 seconds number: 400000 #### sub task { my ($id, $val) = @_; say "Hobo $id started"; $val = $number->incr() for 1 .. 100000; say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 4 ended Hobo 3 ended Hobo 1 ended Hobo 2 ended duration: 2.183 seconds number: 400000 #### #!/usr/bin/env perl use strict; use warnings; use MCE::Hobo; use MCE::Mutex; use MCE::Shared; use Time::HiRes qw(time); use feature qw(say); my $mutex = MCE::Mutex->new( impl => 'Channel' ); my $number = MCE::Shared->scalar(0); my $start = time; MCE::Hobo->create('task', $_) for 1 .. 4; MCE::Hobo->waitall; printf "duration: %0.3f seconds\n", time - $start; say "number: ", $number->get(); sub task { my ($id, $val) = @_; say "Hobo $id started"; for ( 1 .. 100000 ) { $mutex->lock; $val = ++${ $number }; $mutex->unlock; } say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 2 ended Hobo 1 ended Hobo 4 ended Hobo 3 ended duration: 20.343 seconds number: 400000 #### sub task { my ($id, $val) = @_; say "Hobo $id started"; for ( 1 .. 100000 ) { $mutex->lock; $val = $number->incr(); $mutex->unlock; } say "Hobo $id ended"; } __END__ Hobo 1 started Hobo 2 started Hobo 3 started Hobo 4 started Hobo 3 ended Hobo 4 ended Hobo 1 ended Hobo 2 ended duration: 9.702 seconds number: 400000