In C++, is there a way to create - say - 5 threads at the start of the code and then let them wait in the background throughout the entire time the main portion of my code is running? These 5 threads are waiting to be called at varying points in time during the code's main process.
I like these 5 threads to 'contain' a function, so that at some point in the main portion of my code (after preprocessing 5 inputs) I can simultaneously call these 5 threads to asynchronously compute 5 return values of 5 inputs in parallel.
I think I can do this using std::async but I want to avoid the large costs related to creating new 5 threads several times which std::async may do, so I just want these 5 threads to be created at the start of the code and be in the background in standby mode until I get to call them.
std::asyncdoes not guarantee the code is run in a background thread - or that it runs concurrently at all. The specification only requires that it be run asynchronously, which is not the same thing as concurrently. The spec allows forstd::async's user-function to be invoked only when thepromiseis resolved, for example.std::threadis a self-contained execution thread. It is the execution thread itself that calls and executes some code. Your question is unclear.