0

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.

4
  • std::async does 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 for std::async's user-function to be invoked only when the promise is resolved, for example. Commented Jan 14, 2019 at 2:59
  • 1
    I think what you a looking for is a std::condition_variable. The cppreference page on it provides a nice usage example. Commented Jan 14, 2019 at 3:02
  • Threads do not "get called". std::thread is a self-contained execution thread. It is the execution thread itself that calls and executes some code. Your question is unclear. Commented Jan 14, 2019 at 3:02
  • 3
    It sounds like you want a "thread pool". Yes it can be done. But it's kind of broad to explain it all. You could probably write a thesis in the subject. Commented Jan 14, 2019 at 3:06

1 Answer 1

2

A common misconception is in your words: "call a thread". You cannot "call" a thread. It is not a function. It is just a job scheduling concept. You can create a thread and execute a task in the thread environment.

To start a thread, one provides a function pointer and a set of arguments to be execute within the thread. Thread creation is pretty efficient and fits many of threaded implementations well. This is the easiest way of handling threads. So, I suggest that you use it.

What you wanted,is to create a pool of threads of a constant size. In this case, every thread needs to run a job which loops waiting for the data to be ready. A common way is to check for an event using a condition variable, std::condition_variable in c++11. When the input is ready, the main thread signals the variable and a thread can read the data. There is no way to know which thread gets awakened when one signals the variable. So you have to make sure that the inputs are distributed correctly between threads. So, this is a more complicated method which is mostly used to control resources and can provide some performance gain in systems with big number of short tasks.

In any case, be careful how you would use the resuls. You might need to synchronize them using mutexes or other methods.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.