in reply to Using Select and pipes to communicate with threads

Very Cool!

Question: How would you modify this so that the main process could send messages/data to the threads?

I don't have a true multi-processor system, so I can't say whether this is faster than just running one big sequential file, but it is a useful techique for monitoring the output of threads.

My machine has 8 cores (Intel Core i7) and this is the result after running it:

jdrago999@e6510:~$ time perl Desktop/thread-prime.pl ........lots of output here.......... real 0m3.314s user 0m11.540s sys 0m0.490s

Replies are listed 'Best First'.
Re^2: Using Select and pipes to communicate with threads
by zentara (Cardinal) on Sep 02, 2011 at 09:06 UTC
    How would you modify this so that the main process could send messages/data to the threads?

    Pass 2 pipes to each thread? set the second pipe as reader in the thread and writer in the main thread. Probably you would have to stuff the pipe filehandles in a hash in the main thread, so you could keep track of which pipe writes to which thread. I suppose you can run IO::Select in each thread to watch for messages, or setup an eventloop to watch the filehandle, or a basic loop of some sort.

    I will work on an example. :-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh