-1

What is the best data structure to use to do the following:

2 Threads:

  1. 1 Produces (writes) to a data structure
  2. 1 Consumes (reads and then deletes) from the data structure.
  3. Thread safe
  4. Producer and Consumer can access data structure simultaenously
  5. Efficient for large amounts of data
PiousVenom
  • 6,888
  • 11
  • 47
  • 86
user1880826
  • 81
  • 1
  • 5

1 Answers1

0

I wouldn't say that point 4 is impossible, but it is pretty hard, and actually, you should think hard if you really have that requirement.

...

Now that you realized that you don't, the Queue<T> would be what immediately springs to my mind when reading Producer/Consumer.

Let's say you have a thread running ProducerProc() and another running ConsumerProc(), and a method CreateThing() which produces, and a method HandleThing() which consumes, my solution would look something like this:

private Queue<T> queue;

private void ProducerProc()
{
    while (true) // real abort condition goes here
    {
        lock (this.queue)
        {
            this.queue.Enqueue(CreateThing());
            Monitor.Pulse(this.queue);
        }
        Thread.Yield();
    }
}

private void ConsumerProc()
{
    while (true)
    {
        T thing;
        lock (this.queue)
        {
            Monitor.Wait(this.queue);
            thing = this.queue.Dequeue();
        }
        HandleThing(thing);
    }
}

Seeing lock, you realize immediately, that the two threads do NOT access the data structure completely simultaneously. But then, they only keep the lock for the tiniest amount of time. And the Pulse/Wait thing makes the consumer thread immediately react to the producer thread. This should really be good enough.

Jan Dörrenhaus
  • 6,581
  • 2
  • 34
  • 45