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

Threads: why locking is required when using shared variables

by ikegami (Pope)
on Oct 19, 2006 at 18:47 UTC ( #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
Node Status?
node history
Node Type: perltutorial [id://579444]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (16)
As of 2014-09-22 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (200 votes), past polls