Reputation: 18065
I just learned about queues in .NET and I have a few questions.
Let's say that I'm building an application that downloads the HTML of pages and then processes it. Here's how I want it to operate:
How can I implement such a scenario without the possibility of a race condition?
Also, what is the best way to pass the information between queues and threads as described above?
Could you give me some sample code?
Thanks!
Upvotes: 0
Views: 595
Reputation: 1353
There are two ways that come to mind.
The first is to use a collection that implements locking to produce thread safe consistency (such as BlockingCollection as mentioned elsewhere).
The second is to use an immutable collection (such as the ones described by Eric Lippart in Link ) which avoids most of the hassle of thread locking and race conditions in the first place and makes what's ever left over a little more obvious usually.
Upvotes: 0
Reputation: 456607
I recommend BlockingCollection<T>
. It represents a "producer/consumer queue" common in multithreading.
Upvotes: 1