In the first example code, all tasks are launched successfully without any issues. However, in the second example code, only the first task is launched, and the program waits there without executing the remaining lines of code. It seems even if when the the functors of class (A,B,C,D) don't return anything (void), we need to define objects of std::future type and I don't understand why!
// example #1
int main()
{
A a("A");
B b("B");
C c("C");
D d("D");
Controller controller("Controller");
// Resources shared between threads
SharedResource sharedResource;
ControllerResource controllerResource;
std::future<void> taskA = std::async(std::launch::async, a, std::ref(sharedResource));
std::future<void> taskB = std::async(std::launch::async, b, std::ref(sharedResource));
std::future<void> taskC = std::async(std::launch::async, c, std::ref(sharedResource));
std::future<void> taskD = std::async(std::launch::async, d, std::ref(sharedResource));
std::thread thController(controller, std::ref(controllerResource), std::ref(sharedResource));
thController.join();
}
// example #2
int main()
{
A a("A");
B b("B");
C c("C");
D d("D");
Controller controller("Controller");
// Resources shared between threads
SharedResource sharedResource;
ControllerResource controllerResource;
std::async(std::launch::async, a, std::ref(sharedResource));
std::async(std::launch::async, b, std::ref(sharedResource));
std::async(std::launch::async, c, std::ref(sharedResource));
std::async(std::launch::async, d, std::ref(sharedResource));
std::thread thController(controller, std::ref(controllerResource), std::ref(sharedResource));
thController.join();
}