开发者

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

开发者 https://www.devze.com 2023-01-15 08:41 出处:网络
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 p

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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消