[Perl] Thread 사용하기

Perl 2009. 8. 12. 19:06




#!/usr/bin/perl

use Thread;


# ThreadFunc 이라는 함수를 본체로 하는 Thread 생성, 실행
my $thread1 = new Thread \&ThreadFunc;
my $thread2 = new Thread \&ThreadFunc;


# detach : 제어권이 메인thread로 넘어감 (진정한 의미의 Thread)
$thread1->detach;
$thread2->detach;

# join : 제어권이 메인Thread로 넘어가지 않음
#$thread1->join;
#$thread2->join;



# Main Thread
while (1) {
    print "Main Thread\n";
    sleep 1;
}




sub ThreadFunc
{
    # tid = thread id
    my $tid = Thread->self->tid;
   
    while (1) {
        print "sub thread $tid\n";
        sleep int(rand(5));  # 5초 사이의 아무값이나 쉬어줌
    }
}





'Perl' 카테고리의 다른 글

[Perl] Expect 설치  (0) 2009.08.16
[Perl] ssh 접속  (0) 2009.08.13
Posted by bloodguy
,