Tamás Szelei
Tamás Szelei

Reputation: 23941

Can I read and write a FIFO from concurrent threads in .NET?

I'm using a Queue<T> for caching video. The idea is to fill it with data (Enqueue), start playing (Dequeue) and fill back continously as data arrives. Can I do the filling back part from a background thread?

Upvotes: 0

Views: 467

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500923

It sounds like you're looking for a producer/consumer queue. You can do this using Queue<T>, but you'll need to add locking to make sure you never access the queue from multiple threads concurrently.

If you're using .NET 4, Parallel Extensions makes this much easier with IProducerConsumerCollection<T> and BlockingCollection<T> which do all the hard work for you.

Upvotes: 3

Of course, you can, if you lock access to the queue using lock() or using Monitor object.

Upvotes: 0

Related Questions