Where in your code are you checking for whether the variable is locked or not? The locking is "advisory", that means you need to manually check for whether a variable is locked or not. See threads::shared. You need to call lock wherever you want to modify $LockVar:

async { lock $LockVar; $LockVar = 'Haha'; sleep 3; print "Thread 1 +: $LockVar\n" }; async { lock $LockVar; $LockVar = 'Hehe'; sleep 3; print "Thread 2 +: $LockVar\n" };

Personally, I try to stay away from approaches that require locks. Usually, a queue into which I can put new values and a single thread that reads from that queue and updates the master data at opportune times is sufficient. Or simply using a relational database as backend, as that takes care of the consistency for me. But if you're using threads, you most likely did so in the hope of gaining performance with short-lived calculations, which talking to a database usually precludes.

Update: Here is how the locking could work in a self-contained example:

#!perl -w use strict; use 5.010; use threads; use threads::shared; my $LockVar :shared; $LockVar = 'default'; async { lock $LockVar; my $oldLockVar= $LockVar; $LockVar = 'Haha'; sl +eep 3; say "Thread 1: set var from $oldLockVar to $LockVar" }; async { lock $LockVar; my $oldLockVar= $LockVar; $LockVar = 'Hehe'; sl +eep 5; say "Thread 2: set var from $oldLockVar to $LockVar" }; my $TryToReadLockVar = threads->new( {'void' => 'void'}, sub { my $count = 1; while( 1 ) { say "try to modify the Lock var {$count} time"; $count += 1; lock($LockVar); # !!! say "Now setting LockVar"; $LockVar = 'this lock var has been change'; last; } }, ); $TryToReadLockVar->join(); say "LockVar is now $LockVar"; __END__ try to modify the Lock var {1} time Thread 1: set var from default to Haha Thread 2: set var from Haha to Hehe Now setting LockVar LockVar is now this lock var has been change Perl exited with active threads: 0 running and unjoined 2 finished and unjoined 0 running and detached

In reply to Re^3: threads::shared: lock a VARIABLE by Corion
in thread threads::shared: lock a VARIABLE by freakcoco

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.