开发者

Multithreaded Design

I have limited experience designing multi-threaded applications, and was hoping for some general advice on the following simple problem:

An application communicates with a collection of network devices. At regular intervals, and on demand, it must perform a maintenance task on each device. Previously it had done this by calling a method on each device sequentially. However this is now taking too long, so it must be modified to call the method on each device asynchronously.

My initial thoughts are that it needs to call the method for each device asynchronously, have a callback set a variable which indicates that the method is complete, and in the meantime loop through these variables until they all indicate that their corresponding method is complete, so the overall process can return as complete.

开发者_运维技巧

FYI I am programming in C#. I have high level knowledge but little experience of ThreadStart, ThreadPool, etc.


If you are using .NET 4.0, you might want to check these out:

General intro to parallel programming

Parallel.ForEach


Without digging too much into how threading affects your code and how does it behave under the hood on single vs multi-processor machines, you have several alternatives:

1) The classic Thread class, which accepts a delegate to perform its duty. Preferred for lengthy tasks that don't necessarily require status updates.

2) You can use BackgroundWorkers, which expose state events. They use the .NET ThreadPool

3) You can invoke your delegates by your own with BeginInvoke, which will run its task on a .NET ThreadPool thread and allow you to specify a callback to be run upon task completion

4) You can use the new & nice Task Parallel Library, provides you everything you need in a very elegant fashion

Remember that if those tasks you wish to execute "in parallel" share any state, you must serialize access to that using the lock statement (other methods exists, depending on the requirements). Beware of deadlocks!


From written here - have a look a at Parralel.For


You can use a job count, job lock, and shutdown flag. To shut down:

  1. Acquire the job lock.
  2. If the job count is zero, shut down.
  3. Set the shutdown flag.
  4. Release the lock.

To assign a job:

  1. Acquire the job lock.
  2. Increment the job count.
  3. Release the lock.

When finished with job:

  1. Acquire the job lock
  2. Decrement the job count.
  3. If the count is not zero, release the lock and return.
  4. If the shutdown flag is clear, release the lock and return.
  5. Shutdown.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜