Created
August 26, 2011 07:21
-
-
Save horus/1172896 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 threads; | |
| my $num_thr = $ARGV[0] or die "Specify the number of threads first.\n"; | |
| my $rate = $ARGV[1] || 1; | |
| my $contiune = 1; | |
| local $SIG{TERM} = $SIG{INT} = $SIG{HUP} = sub { $contiune = 0 }; | |
| sub thread_func { | |
| my $filename = "test_file_" . shift; | |
| my $sometime = shift; | |
| my $content = 'a'; | |
| open my $fh, '>>', $filename or return; | |
| select((select($fh), $|=1)[0]); | |
| while ($contiune) { | |
| print $fh $content x 50, "\n"; | |
| printf STDERR "[Thread %s] wrote: %s\n", threads->tid, $filename; | |
| $content++; | |
| sleep $sometime; | |
| } | |
| close $fh; | |
| printf STDERR "[Thread %s] finished: %s, trying to clean up.\n", threads->tid, $filename; | |
| unlink $filename; | |
| } | |
| for my $seq (1 .. $num_thr) { | |
| my $t = threads->create(\&thread_func, $seq, $rate); | |
| printf STDERR "[Thread %s] created: %s\n", threads->tid, $t->tid; | |
| } | |
| sleep 10 while $contiune; | |
| while ((my @threads = threads->list) > 0) { | |
| for my $t (@threads) { | |
| if ($t->is_joinable) { | |
| $t->join; | |
| printf STDERR "[Thread %s] thread: %s joined.\n", threads->tid, $t->tid; | |
| } elsif ($t->is_running) { | |
| printf STDERR "[Thread %s] running thread %s not yet ready for joining, sending signal...\n", threads->tid, $t->tid; | |
| $t->kill('SIGINT'); | |
| } else { | |
| printf STDERR "[Thread %s] trying to kill thread %s with SIGKILL\n", threads->tid, $t->tid; | |
| $t->kill('SIGKILL'); | |
| } | |
| } | |
| sleep 1; | |
| } | |
| exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment