http://www.perlmonks.org?node_id=1221594


in reply to PerlIO file handle dup

Hi chris212,

At the time of my last reply, I didn't realize you had made an update. Disclaimer: Oh, btw, I'm not here to push MCE. Please use the module of your liking. Depending on the OS and/or number of workers, MCE::Mutex may run faster than Thread::Semaphore.

Thread::Semaphore

use strict; use warnings; use threads; use Thread::Semaphore; use MCE::Shared; use Time::HiRes 'time'; my $condvar = MCE::Shared->condvar; my $sem = Thread::Semaphore->new; # Start the shared server. Not necessary if Perl has IO::FDPass. MCE::Shared->start; sub test { $condvar->wait; for (1..10000) { threads->yield; $sem->down; $sem->up; } } threads->create('test') for 1..3; $condvar->broadcast(0.5); my $start = time; $_->join for threads->list; printf "duration: %0.03f secs\n", time - $start;

MCE::Mutex::Channel

use strict; use warnings; use threads; use MCE::Mutex; use MCE::Shared; use Time::HiRes 'time'; my $condvar = MCE::Shared->condvar; my $mutex = MCE::Mutex->new; # Start the shared server. Not necessary if Perl has IO::FDPass. MCE::Shared->start; sub test { $condvar->wait; for (1..10000) { threads->yield; $mutex->lock; $mutex->unlock; } } threads->create('test') for 1..3; $condvar->broadcast(0.5); my $start = time; $_->join for threads->list; printf "duration: %0.03f secs\n", time - $start;

MCE::Mutex::Flock

use strict; use warnings; use threads; use MCE::Mutex; use MCE::Shared; use Time::HiRes 'time'; my $condvar = MCE::Shared->condvar; my $mutex = MCE::Mutex->new( impl => 'Flock' ); # Start the shared server. Not necessary if Perl has IO::FDPass. MCE::Shared->start; sub test { $condvar->wait; for (1..10000) { threads->yield; $mutex->lock; $mutex->unlock; } } threads->create('test') for 1..3; $condvar->broadcast(0.5); my $start = time; $_->join for threads->list; printf "duration: %0.03f secs\n", time - $start;

Results

My laptop is a late 2013 Macbook Pro, 2.6 Ghz i7 quad CPU. Each virtual machine is configured with 4 cores.

* 3 threads: CentOS Linux 7.3

Thread::Semaphore 0.386 secs MCE::Mutex::Channel 0.162 secs MCE::Mutex::Flock 0.144 secs

* 3 threads: Windows 7

Thread::Semaphore 0.293 secs MCE::Mutex::Channel 0.499 secs MCE::Mutex::Flock 0.498 secs

* 20 threads: CentOS Linux 7.3

Thread::Semaphore 41.897 secs MCE::Mutex::Channel 0.980 secs MCE::Mutex::Flock 0.702 secs

* 20 threads: Windows 7

Thread::Semaphore 35.521 secs MCE::Mutex::Channel 2.994 secs MCE::Mutex::Flock 3.322 secs

Regards, Mario