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.


In reply to Re: threads::shared - acquire lock on an object that has a socket by aral
in thread threads::shared - acquire lock on an object that has a socket by aral

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.