Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: threads::shared - acquire lock on an object that has a socket

by aral (Acolyte)
on Apr 25, 2014 at 14:09 UTC ( [id://1083791]=note: print w/replies, xml ) Need Help??


in reply to threads::shared - acquire lock on an object that has a socket

Thanks for the feedback. I think I managed to lock a separate private variable of the object and now can use a socket inside the object that is not shared and therefore does not cause a conflict.

This is a fully working perl sample script:

#!/usr/bin/perl use strict; use warnings; use threads; use threads::shared; { package Foo; use threads::shared; use constant INDENT => "\t\t\t\t\t\t\t\t"; sub new { my $class = shift; my %this; $this{_socket} = shift; $this{_sendBuffer} = []; # TBD: Why does this not work? # my %this = { # _socket => shift, # _sendBuffer => [], # }; share ($this{_sendBuffer}); # make this a shared variable t +o allow main thread to lock & manipulate this return bless \%this, $class; } sub serve { my $this = shift; { # scope for lock on $Foo->{_sendBuffer} print INDENT . "object thread: 1st lock attempt\n"; lock ($this->{_sendBuffer}); print INDENT . "object thread: 1st lock acquired..."; push $this->{_sendBuffer}, "datagram " . scalar (@{$this-> +{_sendBuffer}}); sleep (1); print " and released!\n"; # pretend to release lock + before going out of scope } # release lock sleep (1); { # scope for lock on $Foo->{_sendBuffer} print INDENT . "object thread: 2nd lock attempt\n"; lock ($this->{_sendBuffer}); print INDENT . "object thread: 2nd lock acquired..."; push $this->{_sendBuffer}, "datagram " . scalar (@{$this-> +{_sendBuffer}}); sleep (1); print " and released!\n"; # pretend to release lock +before going out of scope } # release lock } } my $foo = new Foo(); my $thr1 = threads->create(sub { $foo->serve() }); { # scope for lock on $foo print "main thread: 1st lock attempt\n"; lock ($foo->{_sendBuffer}); print "main thread: 1st lock acquired...\n"; print " foo send buffer size: " . scalar (@{$foo->{_sendBuffer} +}) . "\n"; sleep (1); print "main thread: 1st lock released!\n"; } # release lock sleep (1); { # scope for lock on $foo print "main thread: 2nd lock attempt\n"; lock ($foo->{_sendBuffer}); print "main thread: 2nd lock acquired...\n"; print " foo send buffer size: " . scalar (@{$foo->{_sendBuffer} +}) . "\n"; sleep (1); print "main thread: 2nd lock released!\n"; } # release lock $thr1->join(); # for some reason, this calls the destructor of +foo (if present): so the thread obtained a copy of foo to work with? print "main thread: object thread joined\n\n"; my $index = 0; foreach my $item (@{$foo->{_sendBuffer}}) { # loop through contents + of sendBuffer print "foo->{_sendBuffer[$index]}: $item\n"; # output sendBuf +fer contents $index++; } undef ($foo); # destroy object

I know, I am not really using a socket here - this sample was designed to find a way to use a "mutex" inside an object without locking the whole object. This is accomplished, and from my larger project I can now use a socket in the object.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found