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:
- Acquire the job lock.
- If the job count is zero, shut down.
- Set the shutdown flag.
- Release the lock.
To assign a job:
- Acquire the job lock.
- Increment the job count.
- Release the lock.
When finished with job:
- Acquire the job lock
- Decrement the job count.
- If the count is not zero, release the lock and return.
- If the shutdown flag is clear, release the lock and return.
- Shutdown.
精彩评论