Reputation: 8310
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.
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:
NetTcpBinding
service and enqueues new requests in the queue. Moreover, it sends any replies via callback.BasicHttpBinding
service and enqueues new requests in the queue.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
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
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