Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Threads: why locking is required when using shared variables

by ikegami (Patriarch)
on Oct 19, 2006 at 18:47 UTC ( [id://579444]=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    my $count     = 100_000;
    my $num_calls = 4;
    ...
    
    inc() for 1..$num_calls;
    print("$sum\n");   # 400000
    
  2. or download this
    use threads;
    use threads::shared;
    ...
    
    $_->join for map { threads->create( \&inc ) } 1..$num_calls;
    print("$sum\n");   # 314813
    
  3. or download this
    +=======================+
    |          CPU          |
    ...
    | save $sum |           |
    | ...       |           |
    +===========+===========+
    
  4. or download this
    use threads;
    use threads::shared;
    ...
    
    $_->join for map { threads->create( \&inc ) } 1..$num_calls;
    print("$sum\n");   # 400000
    
  5. or download this
    ++s     sum = 233564 (expecting 400000)
    s+=1    sum = 143915 (expecting 400000)
    c.=l    length = 248149 (expecting 400000)
    c=c.l   length = 123360 (expecting 400000)
    
  6. or download this
    use v5.8.0;
    
    ...
       print("c=c.l   length = " . length($content) .
             " (expecting " . ($count*$threads). ")\n");
    }
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found