I am using 4 threads to create a few objects using a thread_local
memory pool.
I am using std::vector<std::future<int>>
and std::async(std::launch::async, function);
to dispatch the threads and std::for_each
with t.get
to get their value back. Here's the code:
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';
}
(the construct
and destroy
call allocate
and deallocate
internally.) I get a lot of expected output from the terminal, but somewhere along the lines abort
gets called and the program doesn't complete normally. I can't find out why. I believe that the t.get()
call of a thread that's been started with std::async automatically calls .join
too right?
Using C++17 and Visual Studio 2017. What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire