Passing parameters to a thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm running into a thread problem. The context is a distributed application
constituted of a controller and several satellites performing a given job.

In the controller application, I have to send command requests over the
network to a list of satellites. In order to parallelize request sending, I'd
like to have each request (one per satellite) sent by a single Thread. The
problem is how would a given thread know which satellite he should be taking
care of since it seems like I can't pass parameters to a thread.

My threads can have access to the list (a Hashtable actually) of satellites,
but how would a given thread know exactly which one it should be taking care
of ? I would need, at least, to pass an index to each thread...

Any idea or comment is welcome.
Thanks in advance.
Arno
 
Arno said:
I'm running into a thread problem. The context is a distributed application
constituted of a controller and several satellites performing a given job.

In the controller application, I have to send command requests over the
network to a list of satellites. In order to parallelize request sending, I'd
like to have each request (one per satellite) sent by a single Thread. The
problem is how would a given thread know which satellite he should be taking
care of since it seems like I can't pass parameters to a thread.

My threads can have access to the list (a Hashtable actually) of satellites,
but how would a given thread know exactly which one it should be taking care
of ? I would need, at least, to pass an index to each thread...

See http://www.pobox.com/~skeet/csharp/threads/parameters.shtml
 
Hi,
I think your approach is not object oriented.
Your should manage it so each request is an object with
its own properties and send the thread function from this
object.
Since each object has its own properties your thread is
automatically given all the data required.

Haim
 
Thanks to all of you.

I'm gonna use ThreadPools which seems to fit to my needs as well as
improving my current design.

Haim proposed approach (more Object Oriented) would have also been
interesting if I haven't been so far in my code already :(

Cheers,
Arno
 
IMO, simplest method would be a blocking circular queue. Have your main
thread queue simple class that has your request state (i.e. remote endpoint,
etc) and enqueue it. Then have one worker thread class that gets the queue
in the constructor. Have a Start() methon on the worker class that starts
the ThreadStart entry point. In this method is you main worker loop. Loop
while not stopped. You then dequeue from the queue in the loop and can even
just do a simple blocking send on your socket. Pretty simple and effective
design I think. See my bounded blocking queue at:
http://www.codeproject.com/csharp/BoundedBlockingQueue.asp
I think I may also have a simple server/worker design in the zip. HTH
 
Back
Top