Hi dan -
> -----Original Message-----
> From: dan [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, February 05, 2003 12:43 PM
> To: [EMAIL PROTECTED]
> Subject: Another threading question
> 
> 
> Hey again,
> 
> I managed to get my thread doing timeouts, thanks to everyone who 
> helped me.
> Now I have another dilemma to come across. The timeout sub in the thread
> successfully does timeouts, and removes them accordingly. However, I need
> the thread to print to an already open Socket that it has timed out the
> certain item. I have SOCK open, to which my program connects to 
> the server,
> which was activated in the main program. For some odd reason, I can't get
> the thread to print information to that same socket. I use the Socket.pm
> module. So the main program sends text like:
> send(SOCK,"text to send" . $newline,0);
> I execute this in the thread, and it does absolutely nothing. The server
> doesn't even receive the data. Is there a way to get around this?
> 
> Many thanks
> 
> Dan
> 

Remember that memory between 5.8 threads is *not* shared by
default. You may declare variables 'shared' using the:

  my $whatever : shared = 0;

syntax; you may share arrays and hashes also - BUT there are
strict rules on what can be shared (see perlthtut). *Complex*
$variables (like 'socket' or 'IO::Socket...') CANNOT be shared.
I'm a little weak on the theory here, but, beleive me, it don't
work! :)

What to do? The simplest approach is to set up (connect, bind,
whatever) the socket *before* you start your timeout thread
and pass the socket to the timeout thread as a parameter
in 'new' or 'create' (perlthtut again). Passing parameters seems
to always work even when you cannot share the variable. You
will have to use 'lock' on some dummy variable
socket whenever you use it (in main or any threads).

The other approach is to start a thread that handles the socket
and have any other threads (including main) pass requests and
receive results using Thread::Queue. This approach is harder to
implement and requires some carefull thought to maintain the
correct sequencing, but performance can be enhanced if you
have a large number of threads using the socket because it need not
be locked before each use.

Good luck.

Aloha => Beau.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to