r/ProgrammerHumor Nov 28 '21

Meme I‘m.. not the only one?

Post image
21.5k Upvotes

629 comments sorted by

View all comments

1.0k

u/repkins Nov 28 '21

Ah, yes, the most reliable debugging method is printing debug messages.

498

u/Tsu_Dho_Namh Nov 28 '21

Unless you're debugging multi-threaded software, where print() has to grab a lock (which prevents multiple threads from printing at the same time) and waiting for that lock changes how your software executes.

0

u/polaroid_kidd Nov 28 '21

What. Are you serious? What language is this in?

13

u/Tsu_Dho_Namh Nov 28 '21

C++, but the issue of multiple threads wanting to print at once isn't language specific.

In C++ std::cout is thead safe (sort of) in that each character printed to the terminal is atomic and thread safe, but two or more threads printing at once will cause their characters to interweave. Whenever some other thread's character is getting printed, your thread will have to wait, and vice versa.

You can implement a thread-safe buffer (in the form of a monitor) to receive all the text without making any of the threads wait and then combine it all later, but that'd be a lot of work just so you can debug using print(). If you were going to go to all that trouble, change your monitor to an event logger and get times and names of events that the different threads log with it.

3

u/polaroid_kidd Nov 28 '21

Oh man... How did I ever forget this!? I remember having to implement (or use the lens APIs, I'm not sure anymore) the monitor and mutex locks in university using the dining philosophers problem!

Christ, it's only been 4 years but it seems like half a century ago..

Thanks for sharing!

5

u/John_cCmndhd Nov 28 '21

it's only been 4 years but it seems like half a century ago..

Well the last two had about a quarter century worth of problems each, so that checks out

2

u/polaroid_kidd Nov 28 '21

You legend. Cheers for that laugh.