Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent access to a queue by multiple threads

Tags:

c#

.net

wcf

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:

  • The first thread dequeues and processes the requests in the queue.
  • The second thread instantiates the NetTcpBinding service and enqueues new requests in the queue. Moreover, it sends any replies via callback.
  • The third thread instantiates the 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.

like image 703
enzom83 Avatar asked Feb 06 '12 18:02

enzom83


People also ask

What is concurrent queue?

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.

Is concurrent queue thread-safe?

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.

Can you explain how IOS supports multi threading?

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.

What is difference between thread and queue?

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.


1 Answers

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:

  • http://msdn.microsoft.com/en-us/library/dd267265.aspx
  • http://geekswithblogs.net/BlackRabbitCoder/archive/2011/02/10/c.net-little-wonders-the-concurrent-collections-1-of-3.aspx
  • http://msdn.microsoft.com/en-us/library/dd997371.aspx
  • http://blogs.msdn.com/b/csharpfaq/archive/2010/08/12/blocking-collection-and-the-producer-consumer-problem.aspx
  • http://msdn.microsoft.com/en-us/library/dd267312.aspx
like image 191
Yahia Avatar answered Sep 30 '22 08:09

Yahia