Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I wonder how that 2nd example optimisation would behave with multiple threads, with one thread reading the string and another thread appending to it. Or would that be considered undefined behaviour? Probably an edge case (and likely to be a race condition bug), but still worth thinking about...


That would be UB. In fact C does not know much about threading at all, the result of a function should be "equivalent" to what it would be if the statements were executed in order, unless you use some synchronization primitives.

But if you call a function, even without suppling s as a parameter, instead of simply result += s[i], then that function could have side effects on the string since the char *s is not "restrict".




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: