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


In reply to Threads, processes and sharing variables by perlnwb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.