From the course: Parallel and Concurrent Programming with C++ Part 2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Thread pool: C++ demo

Thread pool: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 2

Start my 1-month free trial

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 opensource 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…

Contents