Итак, я придумал следующий подход:
Код: Выделить всё
int Max{1000000};
//SimResult is some struct with well-defined default value.
std::vector vec(/*length*/Max);//Initialize with default values of SimResult
int LastAdded{0};
void fill(int RandSeed)
{
Simulator sim{RandSeed};
while(LastAdded < Max)
{
// Do some work to bring foo to the desired state
//The duration of this work is subject to randomness
vec[LastAdded++]
= sim.GetResult();//Produces SimResult.
}
}
main()
{
//launch a bunch of std::async that start
auto fut1 = std::async(fill,1);
auto fut2 = std::async(fill,2);
//maybe some more tasks.
fut1.get();
fut2.get();
//do something with the results in vec.
}
Прочитав различные подходы, кажется, что атомарный подход — хороший кандидат, но я не уверен, какие настройки будут наиболее производительными в моем случае? И даже не уверен, что атомная энергия поможет; может быть, нужен мьютекс, защищающий LastAdded?
Подробнее здесь: https://stackoverflow.com/questions/606 ... y-together