Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Help on Understanding Locks in Multithreading

by koti688 (Sexton)
on Feb 25, 2009 at 10:33 UTC ( [id://746217]=perlquestion: print w/replies, xml ) Need Help??

koti688 has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl use threads; use threads::shared; my $foo : shared; $thr = threads->new(\&sub1, "THREAD1 :"); $thr2 = threads->new(\&sub2, "THREAD2 :"); @ReturnData = $thr->join; print @ReturnData; @ReturnData = $thr2->join; print @ReturnData; sub sub1{ my $i; { lock $foo; $foo=0; } for ($i=0;$i<10000;$i++) { print $_[0].$i."\n"; } { lock $foo; $foo=1; } print " Final value :$foo\n"; } sub sub2{ my $i; for ($i=0;$i<5000;$i++) { print $_[0].$i."\n"; } $out = 1; while ($out) { { lock $foo; if ($foo == 1) { $out = 0; } } } for ($i=5000;$i<10000;$i++) { print $_[0].$i."\n"; } }

Hi Mates, can you please tell me , how the locks are been used in this programme.

i am unable to get what is happening here and how threads are working. Any Help is appreciated.

Replies are listed 'Best First'.
Re: Help on Understanding Locks in Multithreading
by zentara (Archbishop) on Feb 25, 2009 at 11:38 UTC
    Threads: why locking is required when using shared variables

    For what it is worth, I use threads without locking, by setting it up so each thread controls it's own output shared variables that are readonly from the outside, and the input shared vars are readonly from inside the thread. It's like setting up "one way streets". The few extra shared variables needed is more than made up for by not needing traffic control (locking).


    I'm not really a human, but I play one on earth My Petition to the Great Cosmic Conciousness
Re: Help on Understanding Locks in Multithreading
by Anonymous Monk on Feb 25, 2009 at 10:55 UTC
      Thanks for the link.

      But i studied that already and i struck with this example.

      I am confused how the $foo variable is locked in the functions.

      Please explain the locking in this code , if you get anything. i am asking very basic things i guess :(

        What are you expecting, and what did you get which doesn't meet with your expectation ?

        FWIW, one wrinkle with threads is that the return context for the thread is set at threads->new time, not on the context of $thr->join(). Your code:

        $thr = threads->new(\&sub1, "THREAD1 :"); ... @ReturnData = $thr->join; ...
        should be:
        ($thr) = threads->new(\&sub1, "THREAD1 :"); ... @ReturnData = $thr->join; ...
        to provide the right context at the right time.

        The fragment:

        { lock $foo; $foo=0; }
        is reasonably plausible. You're locking $foo for just long enough to give it a new value -- so there is no possibility of some other thread attempting to read or change $foo while it's in any intermediate state -- provided they too lock $foo before doing anything with it. (The lock is dropped at the end of the block it is contained in.)

        I note that $foo is not initialised to anything.

        I note that you:

        print " Final value :$foo\n";
        without locking it. But that's probably OK, because $foo is only changed by the same thread.

        I wonder: you're not expecting $foo to work as some kind of semaphore, are you ?

        I am confused how the $foo variable is locked in the functions.
        Actually '$foo' itself isn't locked, execution access is locked. By that I mean that only one thread may be executing in the code blocks that have the 'lock' command at a time. And those blocks just happening to be modifying 'foo' (well, hopefully by design). The locks are controlling/limiting execution flow to a single thread at a time. This stops 2 threads from doing near simultaneous modifications to $foo.

        In the old days this code was called a 'critical code section'.

        It is always better to have seen your target for yourself, rather than depend upon someone else's description.
        I am confused how the $foo variable is locked in the functions.
        Through the magic of perl threads? I'm sorry, I don't understand your question, but I don't think I could explain lock better than perlthrtut.

        Maybe it would be easier for you to understand if you told us where you got that code, what its supposed to do, etc?

        Try using smaller numbers, not 1000/5000

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (9)
As of 2024-04-18 16:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found