upon initial copypasta, this doesn't seem to provide a method of limiting execution to an arbitrary number of threads at a time. I can either make a bunch of exe's, and launch them all concurrently (which would be silly, since it's that exact behaviour that I'm using threads to avoid ;P), or run them in batches, ie:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
int main(void){
unsigned int count=10,loop;
for(loop=0;loop<count;loop++){ //Scope
Execute exe1, exe2, exe3,exe4,exe5,exe6,exe7,exe8;
if(loop<count)exe1.Start("task1");loop++;
if(loop<count)exe2.Start("task2");loop++;
if(loop<count)exe3.Start("task3");loop++;
if(loop<count)exe4.Start("task4");loop++;
if(loop<count)exe5.Start("task5");loop++;
if(loop<count)exe6.Start("task6");loop++;
if(loop<count)exe7.Start("task7");loop++;
if(loop<count)exe8.Start("task8");
} //Deconstruction
cout << "Press Enter to Continue";
cin.ignore(999,'\n');
return 0;
}
|
in which, it will execute 8 threads, wait till they're all done, then execute the other 2. what I'm trying for is something where (in the case of 10 executions desired, as above) it will launch the initial 8, then as each one finishes, launch another until all 10 have been launched, ie:
enter loop->launch 1-8->4 finishes->launch 9->2 finishes->launch 10-> 1,3,5,6,7,8,9,10 finish->exit loop
ideally, the best method would be to keep track of how many threads are running and use that to control it. if i only needed to run 10 at a time, it wouldn't be a problem, but it can easily balloon over 1000 in my program, so it becomes a huge bottleneck.
i could of course use duct tape and do something like writing a "lock" file while the thread is running, then deleting the lock when it's done, and simply counting the number of lock files to tell how many threads are running... but this will add unnecessary overhead.
you may actually have this mechanism somewhere in the code, but I can't see it.
again, much thanks for the help!
EDIT:
I figured out how to accomplish what i want -- something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
string command("");
bool one=false,two=false,three=false,four=false,five=false,six=false,seven=false,eight=false;
unsigned int count=10,loop;
loop=0;while(loop<count){
command.assign("psexec -accepteula -belownormal optipng -zc1-3 -zm1-3 -zs1-3 -f1-3 -i0 -zw 32k \"D:/testing/input/Gantz_v01c01p010{t.png-task1.png\" >nul 2>&1");
if(loop<count && one==false){one=true;boost::thread thread1(worker,&one,command);loop++;}
command.assign("psexec -accepteula -belownormal optipng -zc1-4 -zm1-4 -zs1-3 -f1-4 -i0 -zw 32k \"D:/testing/input/Gantz_v01c01p010{t.png-task1.png\" >nul 2>&1");
if(loop<count && two==false){two=true;boost::thread thread2(worker,&two,command);loop++;}
command.assign("psexec -accepteula -belownormal optipng -zc1-5 -zm1-5 -zs0-3 -f1-5 -i0 -zw 32k \"D:/testing/input/Gantz_v01c01p010{t.png-task1.png\" >nul 2>&1");
if(loop<count && three==false){three=true;boost::thread thread3(worker,&three,command);loop++;}
command.assign("psexec -accepteula -belownormal optipng -zc1-6 -zm1-6 -zs0-3 -f0-5 -i0 -zw 32k \"D:/testing/input/Gantz_v01c01p010{t.png-task1.png\" >nul 2>&1");
if(loop<count && four==false){four=true;boost::thread thread4(worker,&four,command);loop++;}
// if(loop<count && five==false){five=true;boost::thread thread5(worker,&five,command);loop++;}
// if(loop<count && six==false){six=true;boost::thread thread6(worker,&six,command);loop++;}
// if(loop<count && seven==false){seven=true;boost::thread thread7(worker,&seven,command);loop++;}
// if(loop<count && eight==false){eight=true;boost::thread thread8(worker,&eight,command);loop++;}
}
|
as you can see, I'm passing the pointer to the function, and right before the function exits, it flips the bool back to false -- essentially, it's a lock file, but in ram (bool, even), so a minimal performance hit. unfortunately, it means that the number of concurrent threads is essentially hardcoded, but I can add hardware concurrency to the if statement to keep it from executing more threads than the cpu can handle. so really only the maximum number of threads is hardcoded.
much thanks clanmjc, i couldn't have done it without you!