enzom83
enzom83

Reputation: 8310

Concurrent access to a queue by multiple threads

I'm creating an application with a NetTcpBinding service and a BasicHttpBinding one. Through these services, the clients send requests to the application: these requests must be put into a Queue object, that is the queue of inbound request.

  1. Both services must allow concurrent calls from the clients.
  2. This means that both services should access concurrently to the queue in order to add the received requests.
  3. In addition, another thread must access the queue in order to get and process the requests.

I could use ConcurrencyMode.Multiple and so many calls could be taken simultaneously. However, this does not guarantee a concurrent access to the queue. Should I put the two ServiceHost in two different thread? For example:

This is my idea. Since I am almost a newbie, I would be grateful if you could give me some advice. Maybe I should start writing three threads that access the queue concurrently: for the moment the first two threads may enqueue random requests to the queue, while the third thread consume these requests.

Upvotes: 5

Views: 3732

Answers (2)

Dmitry Romanov
Dmitry Romanov

Reputation: 14090

Since you characterize yourself as "I am almost a newbie" I would suggest you more general reading - Threading in C# Joseph Albahari free online article/book. It thoroughly reviews problems and instrumentation of threading in C#.

I assure you, careful reading of parts 3 and 4 (and reviewing part 1 and 2) will give you the tools to solve not only your current problem but a bunch of many other problems that you will meet on the way of multithreading.

If you just want a quick answer to your question, just follow Yahia's answer.

Upvotes: 3

Yahia
Yahia

Reputation: 70369

IF you are on .NET 4 you should look into ConcurrentQueue<T> and BlockingCollection.

Bascially these are thread-safe collections which are implemented for high performance and are mostly lock-free.

BlockingCollection is specifically implemented for Producer-Consumer-Scenarios like the one you describe.

For reference see:

Upvotes: 9

Related Questions