C
C#2y ago
Tandy

❔ Does BlockingCollection TryTake free the consuming thread to do other things?

I'm not too knowledgeable about multi-threading. But I'm trying to use BlockingCollection.TryTake to consume messages like so:
Task.Run(async () =>
{
while (!_queue.IsCompleted)
{
_queue.TryTake(out var command, Timeout.Infinite);
}
});
Task.Run(async () =>
{
while (!_queue.IsCompleted)
{
_queue.TryTake(out var command, Timeout.Infinite);
}
});
While _queue.TryTake is waiting for an item - is it occupying the thread such that other API functions will be impacted? My fear is that I have to create n number of these collections. If each one is permanently blocking a thread, then the server will run out of resources. But if the thread is freed to do other things - like process incoming HTTP requests - before being signaled then I should be safe. The documentation is a little confusing, but I'm hopeful that the "blocked thread" is free to do other work:
The thread is then free to do some other useful work before trying again to access the collection.
https://learn.microsoft.com/en-us/dotnet/standard/collections/thread-safe/blockingcollection-overview#timed-blocking-operations
BlockingCollection Overview
Read about BlockingCollection, a thread-safe collection class in .NET. This class offers features like concurrent adding & taking of items from many threads.
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server