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

Hi, I am newbie in perl, but I have some experience of working with multithreading. I have read about threads, processes in perl , and now a little bit confused about this part of perl.

Firstly , variables are not sharing between threads, expect using extension threads::shared, so what is the purpose of thread if it cannot access parent process memory. As said in perldoc each creation of the thread is copying memory ... So the question here is it creates new processes, as I can see no, but why do we need copy all memory while creating the thread, processes are do the same work, the thread are often lightweight part of process running inside its address space, and of course thread kernel operates not with processes but with threads.


So this implementation seems weird for me


I need to have shared variables between all new threads, but as for perl implementation of thrads I can't see any major advantages in using threads instead forking new process and using some shared memory techniques


What I need ?


I need to create small server that accepts connection, when new connections is being handled it creates new thread/process, but still loops in infinity loop waiting for new client, at this time created threads doing some stuff with remote client. I have ability to communicate with these threads from main or another thread via pipe or some other ipc mechanism


Why do I need shared variables. I need them because for instance I requested third thread to communicate with my main thread, my pipe is open as reader for main thread and as writer for worker thread. So I need to know file handler of pipe. As I can guess this handler will be copied while forking or threading as well, but also I need to modify global variable, which used for signaling threads that they should now, in my case , write to pipe. So my thread always checks the condition of this variable and sees whether value is equal to the thread ID or some other unique value for thread, if so it begins to write to the pipe(in my case).


So I am looking for best solution for implementing this task. I am newbie so I have little experience now. I would be very grateful everyone for help. Thx in advance

  • Comment on Threads, processes and sharing variables

Replies are listed 'Best First'.
Re: Threads, processes and sharing variables
by jeffa (Bishop) on Mar 24, 2015 at 22:47 UTC

    "I need to have shared variables between all new threads ...

    Why not just fork processes instead? Sounds like you want to perform Interprocess Communication and that's what forking is good at.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Threads, processes and sharing variables
by Discipulus (Canon) on Mar 25, 2015 at 12:29 UTC
    I'm not an expert but perlmonks is full of good posts about threads and shared vars: in my experience it will be worth to search for BrowserUk's and zentara's answers, among others.
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
A reply falls below the community's threshold of quality. You may see it by logging in.