Created
January 22, 2019 12:30
-
-
Save hirose31/2a754056248f3de3b544bb5fc4c8aa15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use 5.010_000; | |
use utf8; | |
use Config; | |
use T; | |
use threads; | |
use Time::HiRes qw(sleep); | |
say $^V; | |
printf("%s: %s\n", $_, $Config{$_} // '') for grep /^use.*thread/, keys %Config; | |
say ''; | |
sub worker { | |
for my $i (1..10) { | |
$T::C++; | |
sleep rand(0.5); | |
} | |
printf("%s: %d\n", threads->tid(), $T::C); | |
} | |
say 'before: ', $T::C; | |
for my $i (1..10) { | |
my $th = threads->create('worker'); | |
} | |
while (threads->list()) { | |
if (my @joinable = threads->list(threads::joinable)) { | |
$_->join() for @joinable; | |
} | |
} | |
say 'after: ', $T::C; | |
exit; | |
package T; | |
our $C = 0; | |
__END__ | |
OUTPUT: | |
v5.18.2 | |
use5005threads: | |
useithreads: define | |
usethreads: define | |
before: 0 | |
10: 10 | |
5: 10 | |
7: 10 | |
2: 10 | |
4: 10 | |
1: 10 | |
3: 10 | |
8: 10 | |
6: 10 | |
9: 10 | |
after: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment