in reply to Re^6: Problem in Inter Process Communication
in thread Problem in Inter Process Communication
Also in the model that u have proposed how do i add support for handling multiple queries,i can use the same strategy as i had used before...
Yes. Second and subsequent queries to the same client will just get picked up by the next available worker.
However, if it is important to only issue a single query against a given client at any one time, you'd have to take additional steps to ensure that. If that is the case, speak up and I'll see what I can come up with.
A few comments on your code:
More habit than for any reason I can remember, but the workers will just block on the queue until there is something to do anyway. And I have vague recollections of it making a difference on at least one occasion.
An array is simpler and works better.
You can obtain the process unique thread id using:
my $tid = threads->self->tid;
Where threads->self is a class method that returns the thread handle of teh current thread and ->tid is an instance method that return the thread identifier for the invocant thread handle.
And whilst you were only starting a single thread for now, it better to use the configuration variable $maxnoofThreads to ensure sufficient undefs are stacked to terminate all the threads.
The construct:
$q->enqueue( ( undef ) x $maxnoofThreads );
Says: build a list of $maxnoofThreads undefs and push them onto the queue.
Once all the work items have been processed, each thread will dequeue an undef, and the while loop will terminate naturally. and the thread will terminate when it "falls of the end" of the worker sub.
Ie. Apply locking to a shared variable.
On some systems, printing to the screen will be serialised by the OS or runtime, but you cannot rely on that everywhere. Also, if the output is redirected to a file, the automatic serialisation goes away.
Putting that all together, it looks like this:
use strict; use threads; use threads::shared; use Thread::Queue; my $mtxStdOut : shared; my $maxnoofThreads = 1; my @clientList = qw( client1 client2 client3 client4 client5 client6 client7 client8 client9 client10 client11 client12 ); my $q = new Thread::Queue; my @workers = map { threads->new( \&worker, $q ); } 1 .. $maxnoofThreads; $q->enqueue(@clientList); $q->enqueue( ( undef ) x $maxnoofThreads ); $_->join for @workers; sub worker { my( $Q ) = shift; my $tid = threads->self->tid; while( my $workItem = $Q->dequeue ) { { lock $mtxStdOut; print " workitem from thread $tid -->$workItem\n"; } ## Perform query ## Perform comparison ## Perform output/cleanup } }
That's a very lightly customised template that I use for most threaded perl applications. It's flexible enough that it lends itself to many uses and is tested well enough that it just works in most cases.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^8: Problem in Inter Process Communication
by libvenus (Sexton) on Aug 22, 2008 at 03:13 UTC | |
by BrowserUk (Patriarch) on Aug 22, 2008 at 08:35 UTC | |
by libvenus (Sexton) on Aug 22, 2008 at 08:49 UTC | |
by BrowserUk (Patriarch) on Aug 22, 2008 at 09:26 UTC | |
by libvenus (Sexton) on Aug 22, 2008 at 10:05 UTC | |
| |
by JoeKamel (Acolyte) on Aug 22, 2008 at 12:28 UTC |