Inter thread communication

  • Thread starter Thread starter David Glover
  • Start date Start date
D

David Glover

Hi,

I am a newbie to multi-threading and especially in relation to the
..NET framework. My situation is as follows:

I have a whole number of threads (which are managing communicaion via
a TCP Socket) to a whole bunch of clients (one thread per client).

The situation could arrise where one client needs requests something
from another client. So I would guess that the thread of the
requesting client has to make a call to the thread managing the client
with the requested data. I also assume that the call needs to send the
data being requested - and then the thread will forward this data to
the client, and process the result (perhaps then forwarding the data
to the orriginal requesting thread, or writing the data to the disk).

I have kept a handle to the threads in a hash table in the *main*
thread (using IP address as the key), and the threads all have access
to this hashtable as they are passed a refernce to the parent thread
at their creation (along with the Socket!!)

How would I get the threads sending messages to each other?? Is this
even possible??

Many thanks for any help you can provide on this.

Regards,

David
 
There are a number of methods of communicating complex (meaning not just
semaphores or mutexes) between running processes. One method is to used a
shared memory space (and then a semaphore to indicate which thread has set
data in place and whether it has been read by the parent yet), or (my
personal choice) using delegates and events to pass whatever you want from
one process to another. In the .NET Framework Developer's Guide there is a
sample called "Event Sample." It will take some study but check out
references there to Events and Delegates. Try and detect the process flow
and flow of interrupts...it will come clear.

For a simpler start look at the Threading Tutorial. There are a number of
samples in there.

If you have trouble finding these things try a search on "thread event
delegate"

Good luck.
 
Hi,

Many thanks for that. I have looked into this today, and I have a quick
question:

Would it work if:

If one thread needs to talk to another, it fires an event which is
captured by the thread which contains all the references to the client
threads. This inturn fires an event which is captured by all the client
threads. They do a quick test to see if the event is for them (by
looking at the IP address which will be stored in the event args), and
if it is carry out the required processing.

I was thinking that this would be easier as each client thread will only
have to listen for 1 event (the 1 from the server), as opposed to each
client thread having to register for n-1 events. It also makes the logic
easier when a new client connects to the system.

I am looking for confirmation that this way of doing things is actually
possible.

Many many thanks for your help thus far,

Regards,

David

From: WildHare

There are a number of methods of communicating complex (meaning not just
semaphores or mutexes) between running processes. One method is to used
a
shared memory space (and then a semaphore to indicate which thread has
set
data in place and whether it has been read by the parent yet), or (my
personal choice) using delegates and events to pass whatever you want
from
one process to another. In the .NET Framework Developer's Guide there is
a
sample called "Event Sample." It will take some study but check out
references there to Events and Delegates. Try and detect the process
flow
and flow of interrupts...it will come clear.

For a simpler start look at the Threading Tutorial. There are a number
of
samples in there.

If you have trouble finding these things try a search on "thread event
delegate"

Good luck.
 
Back
Top