As a novice in C++ threads, I am a little bit confused. The std::async, for 1024 function calls, has almost the same duration like the 1024 calls of the same function. My intention is to demostrate (at least for me) that, by using threads, the execution of the 1024 add function, takes less time. Here is my code
#include <chrono>
#include <cstdio>
#include <future>
#include <thread>
struct Timer {
std::chrono::time_point<std::chrono::steady_clock> start, end;
std::chrono::duration<float> duration{};
Timer() {
start = std::chrono::high_resolution_clock::now();
}
~Timer() {
end = std::chrono::high_resolution_clock::now();
duration = end - start;
printf("Duration %f sn", duration.count());
}
};
int add(int a, int b) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
return a + b;
}
int main(int argc, char* argv[]) {
if(argc != 2) {
printf("Usage: %s <n>n where n is 0 or 1nn", argv[0]);
return 1;
}
Timer timer;
if(atoi(argv[1]) == 0) {
for(int i = 0; i < 1024; i++) {
add(i, 1024);
}
}
if(atoi(argv[1]) == 1) {
for(int i = 0; i < 1024; i++) {
std::future<int> result = std::async(std::launch::async, add, i, 1024);
}
}
return 0;
}
and the CMakeLists.txt:
cmake_minimum_required(VERSION 3.29)
project(threads)
set(CMAKE_CXX_STANDARD 20)
add_executable(threads main.cpp)
A have tested the two scenarios and these are the durations:
cmake-build-debugthreads.exe 0 <---- 1024 calls of add in a for loop
Duration 16.136204 s
cmake-build-debugthreads.exe 1 <---- 1024 threads of add in a for loop
Duration 16.129925 s
What is wrong with my code and/or my logic?
SK