Tuesday 27 November 2007

Perl Threads

One of things when dealing with some perl articles that some of the code may be depreciated. The point is taken for threads. The old "Thread" package is mentioned in code snippet. However the perldoc.perl.org reference discourages its use...

For old code and interim backwards compatibility, the Thread module has been reworked to function as a frontend for both 5005threads and ithreads. Note that the compatibility is not complete: because the data sharing models are directly opposed, anything to do with data sharing has to be thought differently. With the ithreads you must explicitly share() variables between the threads. For new code the use of the Thread module is discouraged and the direct use of the threads and threads::shared modules is encouraged instead.

Furthermore, when coming to use the new "threads" package, the first thing I encountered when replacing the above script was a "A thread exited while %d other threads were still running". I don't think the advised solution from the perldoc.perl.org is all that helpful. It says:

A thread (not necessarily the main thread) exited while there were still other threads running. Usually it's a good idea to first collect the return values of the created threads by joining them, and only then exit from the main thread.

Which I followed... Except using join in this case, ran each thread one at a time. The above statement to me implies the problem lay with the other threads exiting. But when I tried to use sleep in the child threads, I still had same issue except naturally I had to wait for the same response. The solution is in fact to ensure the main thread doesn't exit before its child threads complete as demonstrated in this forum.
An example on how to avoid this would have been useful.

Thursday 22 November 2007

Perl Networking

A couple of useful links here are at linuxjournal and perlfect.