I feel like I'm missing something here...
I slightly altered some code to change from using std::thread
to std::async
and noticed a substantial performance increase. I wrote up a simple test which I assume should run nearly identically using std::thread
as it does using std::async
.
std::atomic<int> someCount = 0;
const int THREADS = 200;
std::vector<std::thread> threadVec(THREADS);
std::vector<std::future<void>> futureVec(THREADS);
auto lam = [&]()
{
for (int i = 0; i < 100; ++i)
someCount++;
};
for (int i = 0; i < THREADS; ++i)
threadVec[i] = std::thread(lam);
for (int i = 0; i < THREADS; ++i)
threadVec[i].join();
for (int i = 0; i < THREADS; ++i)
futureVec[i] = std::async(std::launch::async, lam);
for (int i = 0; i < THREADS; ++i)
futureVec[i].get();
I didn't get too deep into analysis, but some preliminary results made it seem that std::async
code ran around 10X faster! Results varied slightly with optimizations off, I also tried switching the execution order.
Is this some Visual Studio compiler issue? Or is there some deeper implementation issue I'm overlooking that would account for this performance difference? I thought that std::async
was a wrapper around the std::thread
calls?
Also considering these differences, I'm wondering what would be the way to get the best performance here? (There are more than std::thread and std::async which create threads)
What about if I wanted detached threads? (std::async can't do that as far as I'm aware)
See Question&Answers more detail:os