Код: Выделить всё
//...
for (int i = 0; i < n; i++) {
//...
auto var1 = func1(vec1, a1);
auto var2 = func1(vec2, a2); // An invocation separated from the one for var1, so
// if the func1 intermediate result starts in 0 and in
// the 1st iteration it gets 1 for the var1 call, then
// that here for var2 it starts in 0 again, and in the
// 2nd iteration continues from 1 for var1, and so on.
}
//...
Код: Выделить всё
//...
std::vector func1(const std::vector& v1, int a, session_id = "__default__");
//...
< /code>
funcs.cpp
//...
std::vector func1(const std::vector& v1, int a, session_id) {
thread_local static std::unordered_map x;
thread_local static std::unordered_map y;
std::string key_ = session_id ? session_id : "__default__";
//thread_local static z // Before the situation to call func1 in more than 1 place.
//thread_local static vr // Before the situation to call func1 in more than 1 place.
int& z = x[key_];
std::vector& vr = y[key_];
for (int i = 0; i < v1.size(); i++) {
z += v1[i];
std::cout
готово. Функция должна быть подготовлена к будущему сценарию, в котором вызовая программа работает в многопоточном случае, в том случае, когда потоки не делятся информацией между ними.
Подробнее здесь: [url]https://stackoverflow.com/questions/79627484/simplifying-development-c-function-retaining-value-across-calls-from-c-or-p[/url]
Мобильная версия