I am quite new to C++ multithreading. I have the following flow of code:
Main Thread:
- Creates a queue
- Creates a Second Thread
- Adds items to the queue
- Ends
Second Thread:
- If queue has items, pops it.
(the second thread exits when a flag is set to exit).
The problem is, once main function finishes adding items to queue, it stops. Adding a .join() after creating the second thread would just make it to hang infinitely.
Can anyone describe a way to work this out?
It seems the second thread is not seeing the 'exit' flag.
Be sure to synchronize access to it or make it a
atomic_bool
, e.g.