Processes are not the only way of structuring a software system; a lightweight alternative is to use threads. This recipe shows how to create and manage threads using the C++ standard library. We've seen that the main advantages of using the C++ standard library are its portability and the fact that it's not dependent on external libraries (for example, Boost).
Creating a new thread
How to do it...
The code we'll write will be the concurrent version of summing up a large vector of integers. The vector is split into two parts; each thread calculates the sum of its part, and the main thread shows the result.
- Let's define a vector of 100,000 integers, and generate random numbers in the main method: