GDB (https://www.gnu.org/software/gdb/ ) is a handy way to debug command line applications. But in the case of applications that are running many threads, it doesn't by default follow a single thread, so as you step through the code it jumps between threads and it's easy to lose track of where you are. The solution is the scheduler-locking command, which forces the stepper to only step through one active thread at a time.
(gdb) set scheduler-locking on
See here for details: https://sourceware.org/gdb/current/onlinedocs/gdb/All_002dStop-Mode.html