I am trying to identify the difference between std::::thread and std:async through a simple experiment.(check std::async Thread Pool functionality)
However, an error occurs during execution.
What's the problem and how can I fix it?
thread test.
void for_print(int num) {
for (int i = 0; i < 1000; i++)
printf("%d Thread : %d\n", num, i);
}
int main(){
std::vector<std::thread> v_thread(2000);
for (size_t i = 0; i < 2000; i++)
v_thread.emplace_back(std::thread(for_print, i));
//join()
for (size_t i = 0; i < 2000; i++)
v_thread[i].join();
std::vector<std::thread>().swap(v_thread);
return 0;
}
async test
void for_print(int num) {
for (int i = 0; i < 1000; i++)
printf("%d async : %d\n", num, i);
}
int main() {
std::vector<std::future<void>> v_async(2000);
for (size_t i = 0; i < 2000; i++)
v_async.emplace_back(std::async(std::launch::async, for_print, i));
for (size_t i = 0; i < 2000; i++)
v_async[i].wait();
std::vector<std::future<void>>().swap(v_async);
return 0;
}