use threads; use threads::shared; use Thread::Queue; my $toBeSentMessages = Thread::Queue->new;#the queue with the messages which are supposed to be sent my @receivedMessages : shared;##the queue with the messages which are received my $result : shared; sub new{ my ($proto, $userSuffix, $noOfThreads, $host, $port ) = @_; my $class = ref($proto) || $proto; for my $i(1..$noOfThreads) { threads->create("opThread", "$userSuffix$i", $host, $port); } my $self = {}; bless ($self, $class); return $self; } sub send{ my ($self, $message)=@_; $ToSentMessages->enqueue($message); } sub receive{ lock (@receivedMessages); cond_wait(@receivedMessages); return (pop(@receivedMessages)); } sub opThread { my ($user, $host, $port)=@_; my $socket=deapi->new($user, $user, $host, $port); print "Start of Child\n"; while () { print "Inside While\n"; sleep(1); my $message=$ToSentMessages->dequeue_nb(); if (defined($message)) { print "Sending Message $message\n"; $api->send($message); } if ($api->can_read()) { print "Receiving OP Message\n"; $result =(deapi::parse($api->receive())); lock (@ReceivedMessages); push(@ReceivedMessages, $result); cond_signal(@ReceivedMessages); } } }