Here is a little program that runs two for loops on separate threads:
#include <iostream>
#include <thread>
void print_one() {
for (int i = 0; i < 1000; i++) {
std::cout << i << "\n";
}
}
void print_two() {
for (int i = 0; i > -1000; i--) {
std::cout << i << "\n";
}
}
int main(){
std::thread t1(print_one);
std::thread t2(print_two);
t1.join();
t2.join();
}
The program follows a pattern in which one for loop runs about 30-400 times (usually about 30-200), after which the other loop gets its "turn" and does the same. The behavior continues until the program exits.
I left mutex out on purpose to show that it's not about locks. In all examples that I've seen on Youtube, the loops usually alternate after every 1-3 iterations. On my PC it's as if two threads simply can't run simultaneously, and one thread has to take time off while the other one is working. I'm not experiencing any performance issues in general, so it doesn't seem like hardware failure to me.
A thread not doing anything while another one has time to execute std::cout and add a new line hundreds of times in a row just doesn't sound right to me. Is this normal behavior?
I have a Ryzen 5 1600 processor, if that makes any difference.