Foggy Bottoms has asked for the wisdom of the Perl Monks concerning the following question:

I previously wrote about a similar subject here and yet I still have a problem. I read the documentation given with threads, but surely I lack knowledge of some sort...

Could someone please tell methe difference between a thread and a process in Win32 and how that corresponds to forks in Linux ?

Secondly when I create a thread in Perl that corresponds to a never-ending sub (ie it's got a while (1) loop)m how can I stop it nevertheless ?

Thanks for your insight.

Replies are listed 'Best First'.
Re: returning from a thread ?
by liz (Monsignor) on Aug 21, 2003 at 11:33 UTC
    ...the difference between a thread and a process...

    If you're using Perl threads, you shouldn't have to worry about whether it is actually implemented using processes or real threads™. That's why they're "Perl threads".

    Having said that, to my knowledge, Linux is the only system where you can actually think of threads as processes, as having seperate program id's (pid's, $$). This allows modules as Thread::Signal to function in that environment. Win32 only knows about real threads and mimics fork() using threads. Other *nixes are somewhere inbetween. Some can fork() and have real threads (Solaris seems to fall in that group). But e.g. on Mac OS X (and presumably other BSD's), threads look as seperate processes, but can not be signalled.

    ...never-ending sub (ie it's got a while (1) loop)m how can I stop it nevertheless?

    Use a shared variable in the while. So:

    while (1) {
    while ($sharedvar) {
    and reset the shared variable in another thread when you want to have the thread in question stop.

    In that respect, you might also want to have a look at Thread::Queue::Monitored and/or Thread::Conveyor::Monitored.

    Hope this helps.


    I forgot one other way to do this. This will only work if you are running under Linux for now. Use Thread::Signal to activate a handler that will do a Thread::Exit. No nice cleanup that way, but definitely effective ;-)

      I didn't manage to use the shared variable "thinggie" - as I don't have the shared package and since I didn't find it on the ActiveState repository...
      Instead I used a file in which I'm writing a byte (0 for inactive 1 for active)( I know you're thinking it's way too slow but it works fine anyhow...)

      However when the new thread starts up, it blocks the main program and I can't do anything else.
      I know someone already posted a node about this but I can't seem to find it anymore (bad luck :~/ )
      If you've any idea, let me know, and thanks again for your help...
        Strange. Which version of Perl are you using on which platform? The main program should not block!


        Not slow, just ugly.. Very, very ugly..
Re: returning from a thread ?
by EdwardG (Vicar) on Aug 21, 2003 at 14:30 UTC

    To answer the Win32 part of your first question:

    An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the process code, including parts currently being executed by another thread.

    Source: MSDN