Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Sharing Package variables across threads

by ikegami (Patriarch)
on Aug 31, 2011 at 20:26 UTC ( [id://923510]=note: print w/replies, xml ) Need Help??


in reply to Sharing Package variables across threads

threads::shared has no effect on variables, package or otherwise.

The question is really whether you can make a shared package variable. share doesn't say it only works on lexical variables.

Let's try it to confirm:

$ perl -Mthreads -Mthreads::shared -E' share($x); $x = 4; async { say $x; ++$x; }->join; say $x; ' 4 5

So yes, you can.

Sharing adds magic to a variable, and thread cloning handles variables with this magic specially. It doesn't care whether the variable is accessible via the symbol table (package variable), via a function's pad (lexical variable), both or neither.

Replies are listed 'Best First'.
Re^2: Sharing Package variables across threads
by metaperl (Curate) on Sep 01, 2011 at 08:15 UTC

      Yes, but since the threads are in the same process, when a new thread is made, are copies made of package variables also if they arent marked as shared? I'm sorry for these "dumb" questions, but the idea of two threads in the same process is a bit confusing to me.

      perlthrtut#Shared And Unshared Data
      by default, no data is shared.
      When a new Perl thread is created, all the data associated with the current thread is copied to the new thread, and is subsequently private to that new thread!
      http://en.wikipedia.org/wiki/Thread_%28computer_science%29
      In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. It generally results from a fork of a computer program into two or more concurrently running tasks. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process. Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources. In particular, the threads of a process share the latter's instructions (its code) and its context (the values that its variables reference at any given moment). To give an analogy, multiple threads in a process are like multiple cooks reading off the same cook book and following its instructions, not necessarily from the same page.
      perlthrtut#What Is A Thread Anyway?
      A thread is a flow of control through a program with a single execution point.

      Sounds an awful lot like a process, doesn't it? Well, it should. Threads are one of the pieces of a process. Every process has at least one thread and, up until now, every process running Perl had only one thread. With 5.8, though, you can create extra threads.

      Come on, bud, try it! You even have the code already, you just need to remove the call to share.

      the idea of two threads in the same process is a bit confusing to me.

      Don't you mean "the idea of threads is a bit confusing to me"?

      Threads are always in the same process. One never deals with threads in other processes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://923510]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2024-04-18 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found