I thought child processes have access to the global variables.

The child process is a copy of the parent process, made at the point of fork(). So any variables (not just globals) will have the same value at the start of the child process as they did in the parent process at that time. But changes made in the child affect only the copy, not the original in the parent.

The same is true for threads, except that in that case there is a special mechanism called sharing that lets you nominate specific variables to be shared across threads, such that a change made in one thread is visible in the other threads (see threads::shared). This is costly, however - access to such variables must be protected by locking - so should be used sparingly if you hope to retain any speed benefit from making an application multi-threaded.

There are other more explicit mechanisms for sharing data between threads or processes - think for example of pipes, shared memory, or databases. Collectively such mechanisms are known as "IPC" (inter-process communication) - there are lots of modules in the IPC:: namespace that may give you ideas.


In reply to Re^3: Why is my counter not updating in a threaded application? by hv
in thread Why is my counter not updating in a threaded application? by cormanaz

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.