Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I need to write a perl program with two threads. Thread 1 should do read and write operation which is a time taking process. And Thread2 should keep on polling till thread1 complete its task. In case if thread1 fails (read or write fails) the program should exit (both the thread should exit). I have tried some thing like below
#! /usr/bin/perl use strict; use warnings; my $libPath; use threads; use threads::shared; my %share_threads; share (%share_threads); my $thread1 = threads->create(\&readWrite); my $thread2 = threads->create(\&isPollable); $thread1->join(); $thread2->join(); sub readWrite { # thread started $share_threads{threads->self->tid()} = 1; my $i=0; //Read and Write while ( $i<=10000) { print "==\n"; $i++; } # thread ended $share_threads{threads->self->tid()} = 0; } sub isPollable { print "Thread2 started\n"; # Polling while ( $share_threads{$thread1->tid()} ) { print "#"; } }
In case if the first thread exits, am getting an error thread1 exited abnormally. How to handle closing or exiting the threads properly. Thread2 should stop if the thread1 is exited? Any help would be highly appreciable.
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Two threaded program
by BrowserUk (Patriarch) on Oct 11, 2011 at 15:24 UTC | |
|
Re: Two threaded program
by zwon (Abbot) on Oct 11, 2011 at 14:44 UTC | |
|
Re: Two threaded program
by thargas (Deacon) on Oct 11, 2011 at 18:29 UTC |