使用 std::vector<std::future<int>> 和 std::async 启动几个线程时中止()
Abort() when starting up a few threads with std::vector<std::future<int>> and std::async
我正在使用 4 个线程使用 thread_local
内存池创建一些对象。
我正在使用 std::vector<std::future<int>>
和 std::async(std::launch::async, function);
来分派线程,并使用 std::for_each
和 t.get
来取回它们的值。这是代码:
struct GameObject
{
int x_, y_, z_;
int m_cost;
GameObject() = default;
GameObject(int x, int y, int z, int cost)
: x_(x), y_(y), z_(z), m_cost(cost)
{}
};
struct Elf : GameObject
{
Elf() = default;
Elf(int x, int y, int z, int cost)
: GameObject(x, y, z, cost)
{
std::cout << "Elf created" << '\n';
}
~Elf() noexcept
{
std::cout << "Elf destroyed" << '\n';
}
std::string m_cry = "\nA hymn for Gandalf\n";
};
struct Dwarf : GameObject
{
Dwarf() = default;
Dwarf(int x, int y, int z, int cost)
: GameObject(x, y, z, cost)
{
std::cout << "dwarf created" << '\n';
}
~Dwarf() noexcept
{
std::cout << "dwarf destroyed" << '\n';
}
std::string m_cry = "\nFind more cheer in a graveyard\n";
};
int elvenFunc()
{
thread_local ObjectPool<Elf> elvenPool{ 229 };
for (int i = 0; i < elvenPool.getSize(); ++i)
{
Elf* elf = elvenPool.construct(i, i + 1, i + 2, 100);
std::cout << elf->m_cry << '\n';
elvenPool.destroy(elf);
}
thread_local std::promise<int> pr;
pr.set_value(rand());
return 1024;
}
int dwarvenFunc()
{
thread_local ObjectPool<Dwarf> dwarvenPool{ 256 };
for (int i = 0; i < dwarvenPool.getSize(); ++i)
{
Dwarf* dwarf = dwarvenPool.construct(i - 1, i - 2, i - 3, 100);
std::cout << dwarf->m_cry << '\n';
dwarvenPool.destroy(dwarf);
}
thread_local std::promise<int> pr;
pr.set_value(rand());
return 2048;
}
int main()
{
std::ios_base::sync_with_stdio(false);
srand(time(0));
std::vector<std::future<int>> vec{ 4 };
vec.emplace_back(std::async(std::launch::async, elvenFunc));
vec.emplace_back(std::async(std::launch::async, elvenFunc));
vec.emplace_back(std::async(std::launch::async, dwarvenFunc));
vec.emplace_back(std::async(std::launch::async, dwarvenFunc));
int term = 0;
try
{
std::for_each(std::execution::par, vec.begin(), vec.end(), [&term](std::future<int>& t)
{
auto ret = t.get();
std::cout << "thread brought me " << ret << '\n';
term += ret;
});
}
catch (const std::exception& ex)
{
std::cout << ex.what() << '\n';
}
std::cout << "Final word = " << term << '\n';
}
(construct
和 destroy
在内部调用 allocate
和 deallocate
。)我从终端得到了很多预期的输出,但在某处 abort
被调用,程序没有正常完成。我不知道为什么。我相信以 std::async 启动的线程的 t.get()
调用会自动调用 .join
,对吗?
使用 C++17 和 Visual Studio 2017。我做错了什么?
您有未定义的行为。您在无效的未来调用 get
。您的期货向量的前 4 项为空 future
。 get
只有在 future::valid
returns 为真时才能调用。
你觉得这行怎么样
std::vector<std::future<int>> vec{ 4 };
是吗?
Default constructor. Constructs a std::future with no shared state.
After construction, valid() == false.
和 here 您可以阅读调用 future::get
时发生的情况:
The behavior is undefined if valid() is false before the call to this
function.
我正在使用 4 个线程使用 thread_local
内存池创建一些对象。
我正在使用 std::vector<std::future<int>>
和 std::async(std::launch::async, function);
来分派线程,并使用 std::for_each
和 t.get
来取回它们的值。这是代码:
struct GameObject
{
int x_, y_, z_;
int m_cost;
GameObject() = default;
GameObject(int x, int y, int z, int cost)
: x_(x), y_(y), z_(z), m_cost(cost)
{}
};
struct Elf : GameObject
{
Elf() = default;
Elf(int x, int y, int z, int cost)
: GameObject(x, y, z, cost)
{
std::cout << "Elf created" << '\n';
}
~Elf() noexcept
{
std::cout << "Elf destroyed" << '\n';
}
std::string m_cry = "\nA hymn for Gandalf\n";
};
struct Dwarf : GameObject
{
Dwarf() = default;
Dwarf(int x, int y, int z, int cost)
: GameObject(x, y, z, cost)
{
std::cout << "dwarf created" << '\n';
}
~Dwarf() noexcept
{
std::cout << "dwarf destroyed" << '\n';
}
std::string m_cry = "\nFind more cheer in a graveyard\n";
};
int elvenFunc()
{
thread_local ObjectPool<Elf> elvenPool{ 229 };
for (int i = 0; i < elvenPool.getSize(); ++i)
{
Elf* elf = elvenPool.construct(i, i + 1, i + 2, 100);
std::cout << elf->m_cry << '\n';
elvenPool.destroy(elf);
}
thread_local std::promise<int> pr;
pr.set_value(rand());
return 1024;
}
int dwarvenFunc()
{
thread_local ObjectPool<Dwarf> dwarvenPool{ 256 };
for (int i = 0; i < dwarvenPool.getSize(); ++i)
{
Dwarf* dwarf = dwarvenPool.construct(i - 1, i - 2, i - 3, 100);
std::cout << dwarf->m_cry << '\n';
dwarvenPool.destroy(dwarf);
}
thread_local std::promise<int> pr;
pr.set_value(rand());
return 2048;
}
int main()
{
std::ios_base::sync_with_stdio(false);
srand(time(0));
std::vector<std::future<int>> vec{ 4 };
vec.emplace_back(std::async(std::launch::async, elvenFunc));
vec.emplace_back(std::async(std::launch::async, elvenFunc));
vec.emplace_back(std::async(std::launch::async, dwarvenFunc));
vec.emplace_back(std::async(std::launch::async, dwarvenFunc));
int term = 0;
try
{
std::for_each(std::execution::par, vec.begin(), vec.end(), [&term](std::future<int>& t)
{
auto ret = t.get();
std::cout << "thread brought me " << ret << '\n';
term += ret;
});
}
catch (const std::exception& ex)
{
std::cout << ex.what() << '\n';
}
std::cout << "Final word = " << term << '\n';
}
(construct
和 destroy
在内部调用 allocate
和 deallocate
。)我从终端得到了很多预期的输出,但在某处 abort
被调用,程序没有正常完成。我不知道为什么。我相信以 std::async 启动的线程的 t.get()
调用会自动调用 .join
,对吗?
使用 C++17 和 Visual Studio 2017。我做错了什么?
您有未定义的行为。您在无效的未来调用 get
。您的期货向量的前 4 项为空 future
。 get
只有在 future::valid
returns 为真时才能调用。
你觉得这行怎么样
std::vector<std::future<int>> vec{ 4 };
是吗?
Default constructor. Constructs a std::future with no shared state. After construction, valid() == false.
和 here 您可以阅读调用 future::get
时发生的情况:
The behavior is undefined if valid() is false before the call to this function.