|P is for Practical|
threads::shared - acquire lock on an object that has a socketby aral (Novice)
|on Apr 25, 2014 at 11:59 UTC||Need Help??|
aral has asked for the
wisdom of the Perl Monks concerning the following question:
I am trying to serve multiple TCP connections and encapsulate the server for each connection into a class handling its own socket.
1) The main thread starts a TCP connection server, to accept incoming connections
My main application is actually distributing a data stream onto several TCP clients. Therefore, my main thread must serve a send buffer with data to be used by the connection handler threads. For cleaner coding (and due to the low total number of connections), I decided to create a separate send buffer for each connection.
The main thread and the connection handler threads need to acquire a lock on the connection object's send buffer. Therefore I make the objects blessed self-reference shared, and can acquire a lock on the objects reference both from the main thread as well as from within the objects methods (rudimentary object example code below).
Problem: the TCP connection handler gets an open socket from the IO::Socket::INET accept() method. When I try to pass this open socket as an argument to my object constructor, the interpreter complains about an Invalid value for shared scalar in the constructor, because apparently it is not allowed to share a socket variable.
I am somewhat at a loss, because I don't actually want to share the *socket* itself, I simply want to acquire a lock on the buffer variables of my connection object. Is there another way to do this for thread-safe handling of the send buffer per object?
Thanks in advance!
Example for connection class: