Thanks guys.
I like the look of Chris's queue idea, so I tried it ... but still no
joy. Here's the test code. did I screw up ... ?
i'll post this over at perl monks too as I saw some related discussion there.
regards
Daniel
==========
use strict;
use warnings;
use threads;
use Thread::Queue;
use IO::Socket qw(:DEFAULT :crlf);
use Time::HiRes qw( usleep );
my $sock = new IO::Socket::INET (LocalAddr => "localhost",
LocalPort => 1000,
Proto => 'tcp',
Listen => 1,
Reuse => 1);
my $SOCK;
my $rq;
sub sockout{
my $r = shift;
print $SOCK "$r\r\n";
}
sub chatter{
while (1){
usleep(1000 * 1000);
sockout "talkin to myself ...";
}
}
sub reply_thread{
while (1){
while ($rq->pending){
print "rt pr\n";
print $SOCK $rq->dequeue;
};
usleep(1000 * 100);
}
}
$SOCK = $sock->accept();
$rq = new Thread::Queue;
my $reply_thread = threads->create(\&reply_thread);
my $chatter_thread = threads->create(\&chatter);
sockout("CONNECTED!");
while (my $line = <$SOCK>){
sockout("You said ".$line);
}
On 10/27/06, Chris Wagner <[EMAIL PROTECTED]> wrote:
> At 11:59 AM 10/27/2006 +0200, Daniel McBrearty wrote:
> >Now I want to run a thread inside this app that sends back info
> >regularly. It needs to run in a seperate thread, and be startable and
> >stoppable, which I have working. I don't care if the responses from
> >the main thread and the reporter thread lines are mixed up ... ie
>
> One simple thing u can do is to have a dedicated thread handle the sockets
> and have the other threads communicate with it via a message queue. This
> way u never have to worry about thread collisions on the socket. Any old
> thread that wants to talk just writes into that queue and the socket thread
> checks it on a regular basis to see if anything needs to be sent. U can
> even have it write back to the calling thread any return/status information
> about the socket send.
>
>
> --
> REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
> "...ne cede malis"
>
> 00000100
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
--
Daniel McBrearty
email : danielmcbrearty at gmail.com
www.engoi.com : the multi - language vocab trainer
BTW : 0873928131
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs