access to the stdout stream hencethe output of this script is similar to that of the original versionexcept that standard output text is never mangled by overlapping printsc:\pp \system\threadsthread-count-mutex py [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = main thread exiting though somewhat platform-specificthe order in which the threads check in with their prints may still be arbitrary from run to run because they execute in parallel (getting work done in parallel is the whole point of threadsafter all)but they no longer collide in time while printing their text we'll see other cases where the lock idiom comes in to play later in this -it' core component of the multithreading model waiting for spawned thread exits besides avoiding print collisionsthread module locks are surprisingly useful they can form the basis of higher-level synchronization paradigms ( semaphoresand can be used as general thread communication devices ss for instanceexample - uses global list of locks to know when all child threads have finished ss they cannothoweverbe used to directly synchronize processes since processes are more independentthey usually require locking mechanisms that are more long-lived and external to programs ' os open call with an open flag of o_excl allows scripts to lock and unlock files and so is ideal as cross-process locking tool see also the synchronization tools in the multiprocessing and threading modules and the ipc section later in this for other general synchronization ideas threads