in reply to Re^6: Problem in Inter Process Communication
in thread Problem in Inter Process Communication

I completely agree with BrowserUk. A much simpler approach can be used, and the one he proposes sounds good (not that he needs me to say that).

just a couple more notes, if by "multiple queries", you mean multiple actions on the same server, just pass yourself in an array of the actions you need to perform as part of the queue.

on that note, to even further simplify, you could actually get away with avoiding all mutex's, locks, etc, by setting up an output queue, and pushing all the results into it thus only the scripts main portion (thread 0) is accessing the file.

  • Comment on Re^7: Problem in Inter Process Communication

Replies are listed 'Best First'.
Re^8: Problem in Inter Process Communication
by BrowserUk (Patriarch) on Aug 21, 2008 at 11:54 UTC

      Hi browserUK,

      is there some problem in the below code :-

      use strict; use threads; use threads::shared; use Thread::Queue; my @clientList = qw( client1 client2 client3 client4 client5 client6 ); my @queryList = qw( query1 query2 query3 query4 query5 query6 ); my $maxnoofQueryThreads = 15; my $maxnoofCompThreads = 30; my $globalQ = new Thread::Queue; my $queryBossID = threads->new(\&queryBoss,$globalQ); my $compBossID = threads->new(\&compBoss,$globalQ,); $queryBossID->join; $compBossID->join; sub queryBoss { my $q = new Thread::Queue; my $globalQ = shift; my @queryworkers = map {threads->new( \&queryworker, $ +q,$globalQ);} 1 .. $maxnoofQueryThreads; $q->enqueue(@queryList); $q->enqueue( ( undef ) x $maxnoofQueryThreads ); $_->join for @queryworkers; } sub compBoss { my $globalQ = shift; my @compworkers = map {threads->new( \&compworker, $globalQ);} 1 .. +$maxnoofCompThreads; #$q->enqueue(@queryList); $globalQ->enqueue( ( undef ) x $maxnoofCompThreads ); $_->join for @compworkers; } sub compworker { my $globalQ = shift; my $tid = threads->self->tid; while(my $workItem = $globalQ->dequeue) { print " the file is $workItem \n"; #start comparison on the file and dump result in a new file and de +l the file } } sub queryworker { my( $Q ) = shift; my $globalQ = shift; my $tid = threads->self->tid; while( my $workItem = $Q->dequeue ) { for( my $i = 0; $i < @clientList; $i++) { #lock $mtxStdOut; print " workitem from thread $tid -->$workItem\n" +; ## Perform query ## worked on one client for query signal compBoss to start +doing comp ## so enqueue the file location on dum +p my $fileLoc = "$workItem"."$clientList[$i]"; $globalQ->enqueue($fileLoc); } } }
        is there some problem in the below code

        Yes. You cannot have two different types of worker feeding off the same queue. How will each know whether what it pulled off the queue is for it or for the other type of worker?

        You would at least need to use two queues, which isn't a problem and can be an effective way of decoupling two or more types of processing. But you still need to explain why you think this is a good idea? What are you hoping to gain from doing so?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.