DEV Community


Discussion on: std::cout is not printing in console/terminal

linuxrocks2000 profile image

I figured out my issue - the thread never deatched or joined. On a completely unrelated note, is it considered bad programming to have an infinite while loop in a thread?

lucpattyn profile image
Mukit, Ataul Author

If your thread doesn't go to sleep mode because of waiting for I/O or probably waiting for a result, then
definitely you have to put a sleep to make sure the thread doesn't hog the cpu.
You can have a 50ms or 100ms (millisecond) sleep time when you are executing the infinite loop.

However, there are certain OS calls which automatically puts the thread to sleep mode to be activate later, in that case putting a manual sleep is not necessary.

Depending on the situation, having an infinite loop in a thread is not a bad thing, as long as you ensure you send the thread a message through an event or setting off a variable to quit when the main program quits.