>>> critical section or semaphore

There are a number of constructions you can use for thread synchronisation - those are two. Both of them can be used to prevent one thread screwing around with common memory at an inopportune moment.

Cela will find that his simple program will fail soon! The thing with threads is that they can be interupted for another one to start at any time, including part way through a printf()!

FOO
BAR
FBAR
OO
BAFOO
R

and so on. A critical section, for example, can stop that.