ThreadSafeQueue<someBigAndCostlyShit> q;
class someBigAndCostlyShit {
someBigAndCostlyShit() {} //does this prevent eng from constructing?
void initialize()
{
eng.seed(currentTime());
}
~someBigAndCostlyShit
{
q.enqueue(this);
}
std::mt19937_64 eng;
}
void threadfunc()
{
thread_local someBigAndCostlyShit a;
std::optional<someBigAndCostlyShit> val = q.pop();
if(val.has_value())
{
a = val.value();
}
else
{
a.initialize();
}
}
I want to implement a class that will cache itself in a queue when a thread exits to prevent constructing it again when a new thread calls threadfunc, how does this look
I'm afraid that thread_local someBigAndCostlyShit a; will be expensive anyways because of default constructor to mt19937_64
I'm not sure which random number generator to use and if my solution even makes sense
I want to have a random function from x to y but my server is async and I guess a lot of new threads can spawn and often die, so creating these all the time would be costly
thats why I wanna cache them