"usage of concurrentqueue<strongbox<t>>" Code Answer


the reminder of the dangers of premature optimization are in the comments, so i will address the semantics of what's going on here.

like the article points out, the concurrentqueue can hold on to references of some things that have already gone through it. i learned it as 'a few dozen' and the article says it is no more than 31, which seems to gel pretty nicely. if the queue is tracking big objects, like your 2000x2000 bitmaps, that can theoretically become a problem. it depends on what the rest of your program is doing, of course.

wrapping it in a strongbox<t> helps because the only thing strongbox does is hold onto a reference to something else. therefore, a strongbox has a very tiny footprint, and whatever it holds will go out of scope and (theoretically) get gc'd quicker.

since strongbox has all the content of diet soda, you're kind of overthinking its usage. you literally just load up the value field with some t and then reference it later. it looks a little like this:

var boxedbitmap = new strongbox<bitmap>(new bitmap(1,1));
var bitmap = boxedbitmap.value;

or alternatively:

var boxedbitmap = new strongbox<bitmap>();
boxedbitmap.value = new bitmap(1,1);
var bitmap = boxedbitmap.value;

seriously, the implementation of this class if you pop it open in reflector is like 5 lines.

this being the case, your usage of concurrentqueue<t> is not really any different from the usage of concurrentqueue<strongbox<t>>. you'll simply tack on .value before you send the resource to its destination thread. this did help a company i worked for reduce the memory imprint of a massive multithreaded analysis service by quite a bit by simply passing around a reference to a deterministic tool instead of passing the entire tool around, but your mileage may vary - i am not clear on what ramifications it would have if you were passing something to be mutated and then used by something else.

By Reza GH on June 12 2022
Only authorized users can answer the Search term. Please sign in first, or register a free account.