in reply to Re: threads::shared: lock a VARIABLE
in thread threads::shared: lock a VARIABLE
So that's how it is, it will waiting for that.
I changed the code like this:
#!/usr/bin/perl use utf8; use feature qw( say ); use threads; use threads::shared; use Data::Dumper; use Storable qw ( freeze thaw ); my $Clock = threads-> new( {'context' => 'void'}, sub { my $count = 0; while(1){ say "programs implemented {$count} sec"; sleep 1; $count += 1; } } ); my $LockVar :shared; $LockVar = "default"; my $LockArray :shared; $LockArray = qw( You cannot lock the individual elements of a cont +ainer variable ); $Lock3Sec = threads->new( {'void' => 'void'}, sub { { say "the lock var default value is {$LockVar}"; lock($LockVar) or warn "cannot lock the var because {$ +!}\n"; sleep 3; say $LockVar; } }, ); $TryToReadLockVar = threads->new( {'void' => 'void'}, sub { my $count = 1; LINE: while( $LockVar eq 'default'){ say "try to modify the Lock var {$count} time"; $count += 1; $LockVar = 'this lock var has been change' or redo LIN +E; say "modify done"; } }, ); $TryToReadLockVar->join(); $Lock3Sec->detach(); $Clock->detach(); sleep 3;
And the output is:
programs implemented {0} sec the lock var default value is {default} try to modify the Lock var {1} time modify done programs implemented {1} sec programs implemented {2} sec programs implemented {3} sec this lock var has been change
It doesn't seem like waiting properly
The previous version of the program was all over in less than a second!
Why it did not wait for the $Lock3Sec for three seconds, just unblock him $LockVar?
Or It kill $Lock3Sec?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: threads::shared: lock a VARIABLE
by Corion (Patriarch) on Aug 23, 2016 at 09:54 UTC |