I have an array of std::thread
objects that it doesn't matter what order they operate in and what order they rejoin the main thread. I've tried using
for(int i = 0; i < noThreads; ++i)
{
if(threads[i].joinable())
threads[i].join();
}
but that seems to make them operate "in order", of course that could be the fact that i'm reaching that conclusion from the fact that the console output from the threads is happening in the order that I dispatch them in (as in all of the output from thread #1 then all of the output from thread #2). I have also attempted threads[i].detach()
, but I don't know the execution time of each thread, so I can't pause the program until they have finished.
The work that each thread is doing is:
int spawn(const char* cmd)
{
FILE *fp = popen(cmd, "r");
char buff[512];
if(vFlag == 1)
{
while(fgets(buff, sizeof(buff), fp)!= NULL)
{
printf("%s", buff);
}
}
int w = pclose(fp);
if(WIFEXITED(w))
return WEXITSTATUS(w);
else
return -1;
}
where the command being executed is "./otherApp.elf"
. Bearing in mind that the current fact that it is being printed out to console will change to being output to various files, how do I dispatch the threads so that I can make them execute then rejoin out of order?