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.
A concurrent queue is basically a queue which provides protection against multiple threads mutating its state and thus causing inconsistencies. A naive way to implement a concurrent queue may be to just slap locks in its enqueue and dequeue functions when they try to modify the head and tail.
ConcurrentQueue is a thread-safe FIFO data structure. It's a specialized data structure and can be used in cases when we want to process data in a First In First Out manner.
Multithreading is an implementation handled by the host operating system to allow the creation and usage of n amount of threads. Its main purpose is to provide simultaneous execution of two or more parts of a program to utilize all available CPU time.
A message queue is a data structure for holding messages from the time they're sent until the time the receiver retrieves and acts on them. Generally queues are used as a way to 'connect' producers (of data) & consumers (of data). A thread pool is a pool of threads that do some sort of processing.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With