- 论坛徽章:
- 95
|
原帖由 Perl_Er 于 2009-7-1 19:28 发表 ![]()
这样好像不行了,好像是加锁的原因,帮忙看看怎么处理?
这是我写的:
- #!/usr/bin/perl
- use strict;
- use warnings;
- use threads;
- use threads::shared;
- my $thread_num :shared = 0;
- sub start_thread {
- print "Thread ", threads->tid(), " started, current thread numbers $_[0]\n";
- {
- lock($thread_num);
- $thread_num--;
- cond_signal($thread_num);
- }
- }
- for (1 .. 200) {
- {
- lock($thread_num);
- cond_wait($thread_num) until $thread_num < 10;
- my $t = threads->create(\&start_thread, $thread_num + 1);
- $t->detach();
-
- $thread_num++;
- }
- }
复制代码
很奇怪的是,启动的线程的总数并不是 200 个。
使用 is_joinable 的例子:
- #!/usr/bin/perl
- use strict;
- use warnings;
- use threads;
- my $thread_num = 0;
- sub start_thread {
- print "Thread ", threads->tid(), " started, current thread number $_[0]\n";
- }
- for (1 .. 200) {
- if ($thread_num >= 10) {
- for my $t (threads->list(threads::joinable)) {
- $t->join();
- $thread_num--;
- }
- redo;
- }
- threads->create(\&start_thread, $thread_num + 1);
- $thread_num++;
- }
- for my $t (threads->list()) {
- $t->join();
- }
复制代码
[ 本帖最后由 MMMIX 于 2009-7-1 21:09 编辑 ] |
|