Skip to content

Instantly share code, notes, and snippets.

@hirose31
Created January 22, 2019 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirose31/2a754056248f3de3b544bb5fc4c8aa15 to your computer and use it in GitHub Desktop.
Save hirose31/2a754056248f3de3b544bb5fc4c8aa15 to your computer and use it in GitHub Desktop.
#!/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