Print in Order in C++

Overview

Printing in order is another requirement in lots of programming tasks. If we look at this article from the perspective of what is called Concurrency in programming, Printing in order is a good example of concurrency implementation.

Thought Process

This code is executed using the concurrency functionality of the C++ programming language. Concurrency refers to the idea of executing several tasks at the same time. This can be achieved in a time-shared manner on a single CPU core (implying 'Multitasking') or in parallel in the case of multiple CPU Cores (Parallel Processing).

Since the requirement is to hold a task while the other thread is running, using the promise functionality in C++, multithreading can be implemented to hold a thread while the other thread is running.

 thread1.get_future().wait()

Conclusion

In this task, the concept of concurrency and multithreading was implemented in C++. This finds great application in state machine implementation and also parallel programming. It makes use of the opportunity of having multiple cores on modern-day computers.