From the course: Complete Guide to Parallel and Concurrent Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Thread pool: C++ demo

Thread pool: C++ demo

- [Instructor] C++ doesn't include thread pools as part of the C++ standard template library. So once again, we'll be using the open source Boost C++ libraries for this example for its thread pool class. To demonstrate using a thread pool, we've created this program which defines a function named vegetable_chopper on line six that simply prints a message that includes the current thread's ID number and a vegetable ID number, which gets passed as an input to the function. Down in the main section, we use a for loop on line 12 to create and start 100 vegetable chopper threads, followed by a second for loop to wait for them all to finish and join. If I run this program, it creates 100 separate threads to chop the 100 vegetables. We can see that the 100 threads are unique because they each have a different thread ID number. Now let's accomplish the same task of chopping 100 vegetables, but do so with only a handful of threads in a thread pool. First, we'll replace the include statement…

Contents