Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the 5.005 perl threads to create a new thread everytime i receive a connection to a server.

Within the thread, I create a parser object like:

my $parser = MIME::MyParser;

where MyParser is my subclass of MIME::Parser. There are no global variables in my subclass..

The problem is this: even though I have no global variables in my program, the threads share variables.

The Parser modules creates some instances of other objects, like MIME::Head, and MIME::Entity, etc...

And as more threads are created, these objects appear to append to the old values of them, even though the 'constructors' of the classes are called..

I had this problem with my subclass of Parser with a key of $self, and I just undef the important ones before a new one was created, and that seemed to work..

I can't figure out why the objects aren't only in the scope of the threads, and how to fix that ...

help?

Replies are listed 'Best First'.
Re: Perl Threads
by eduardo (Curate) on Jun 21, 2000 at 22:42 UTC
    Ugh! there are some leasons that I have learned with perl. The main lesson being: NEVER USE PERL THREADS there, i feel better now. Rather humorously, even the perl threads example in man perlthrtut does not work (it causes segmentation faults, core dumps, and other assorted goodies.) So unless you have an AMAZINGLY good reason to use them, dont. As far as I understand it, there are issues and problems with the implementation, and both the API and implementation are subject to change at completely random intervals. If you really need process level parallelism, may I suggest you spend some time learning fork?
Re: Perl Threads
by hel0 (Novice) on Jun 22, 2000 at 00:48 UTC
    Yes, perl threads are horribly unstable, and should be avoided for anything real. My guess as to what's causing your problem is that MIME::Parser isn't threadsafe. You may not use any global variables but MIME::Parser could be doing something naughty under the hood. Check the source! However, chances are you won't be able to get threads to do anything worthwhile. Fork is an option, and so is POE(perl object environment). Happy segfaulting!
Re: Perl Threads
by Aighearach (Initiate) on Jun 22, 2000 at 14:46 UTC
    First of all, you are not using the current version of perl. You should upgrade to 5.6.0. But, perl threads are still a development item. This is started very clearly in the docs. That is NOT a normal warning for a perl feature! The suggestion is, that it is included for people who like to hack the perl interpretter to play with it, so as to get it working. I don't know about in the version you have, but I know the 5.6.0 documentation clearly states not to ever use perl threads in a production environment, or in anything else that you expect to work.
    Paris Sinclair    |    4a75737420416e6f74686572
    pariss@efn.org    |    205065726c204861636b6572
    I wear my Geek Code on my finger.
    
Re: Perl Threads
by Anonymous Monk on Jun 22, 2000 at 19:42 UTC
    I said 5.005 threads, because you have the option of using them or 'ithreads', which are the new ones that come with huge warnings. Though I must thank all of you for your, ah-hem, advice. I appreciate your suggestions to look at the source code. Also, to spend some time learning fork. Me. When I ask you how to build a bridge, why do you tell me what I really need is a boat?
      Because we've seen many bridges on this river. Usually, we see them float past after washing out...

      Let me quote the start of the thread tutorial in version 5.6.0:

      WARNING: Threading is an experimental feature.  Both the interface
                 and implementation are subject to change drastically.  In fact, this
                 documentation describes the flavor of threads that was in version
                 5.005.  Perl 5.6.0 and later have the beginnings of support for
                 interpreter threads, which (when finished) is expected to be
                 significantly different from what is described here.  The information
                 contained here may therefore soon be obsolete.  Use at your own risk!
      

      The river is rising... better grab an oar.

      Paris Sinclair    |    4a75737420416e6f74686572
      pariss@efn.org    |    205065726c204861636b6572
      I wear my Geek Code on my finger.