The problem is that since you can use std:bind in a loop with an arbitrary number of iterations, the storage cannot be inline. It doesn't appear to be on the heap, since it doesn't appear to generate memory leaks. It seems, then, that it must be on the stack - SOME stack. I have an application which could easily do hundreds of thousands of these binds setting up calls to a threader. Since the caller could presumably go out of scope while the threader is still working, and since the threader needs the bind until it executes the job, that information must be stored SOMEWHERE. Where is it, and how do I clean it up when the binds have served their purpose?
Example:
std::function<void()> *Jobs;
Jobs = new std::function<void()> Jobs[nJobs];
for (i=0; i<nJobs; ++i)
{
Jobs[i] = std::bind (funtion, args, ...);
}
ThreadPool->QueueJobs (Jobs, nJobs); // store POINTERS to jobs
ThreadPool->WaitForCompletion();
delete Jobs;
Does this look like it will work?
Does this look like it will work?
- try it. See if it does. If it does, ask on Code Review.SE to review it (they will want the full application - not prototype code). If it doesn't, work out a good question and ask it on Stack Overflow.Jobs
is an array, you shoulddelete[]
notdelete
. I think you get away with sorting pointers in QueueJobs because you WaitForCompletition before deleting it. But I would suggest having QueueJobs take a copy, to avoid the possibility of the pointers being deleted. A better solution would be to replace your array with std::vector which will manage the memory for you.