the work done so far by another whose operations are still in progress the extent to which this becomes an issue varies per applicationand sometimes it isn' an issue at all but even things that aren' obviously at risk may be at risk files and streamsfor exampleare shared by all threads in programif multiple threads write to one stream at the same timethe stream might wind up with interleavedgarbled data example - of the prior section was simple demonstration of this phenomenon in actionbut it' indicative of the sorts of clashes in time that can occur when our programs go parallel even simple changes can go awry if they might happen concurrently to be robustthreaded programs need to control access to shared global items like these so that only one thread uses them at once luckilypython' _thread module comes with its own easy-to-use tools for synchronizing access to objects shared among threads these tools are based on the concept of lock--to change shared objectthreads acquire lockmake their changesand then release the lock for other threads to grab python ensures that only one thread can hold lock at any point in timeif others request it while it' heldthey are blocked until the lock becomes available lock objects are allocated and processed with simple and portable calls in the _thread module that are automatically mapped to thread locking mechanisms on the underlying platform for instancein example - lock object created by _thread allocate_lock is acquired and released by each thread around the print call that writes to the shared standard output stream example - pp \system\threads\thread-count-mutex py ""synchronize access to stdoutbecause it is shared globalthread outputs may be intermixed if not synchronized ""import _thread as threadtime def counter(myidcount)for in range(count)time sleep( mutex acquire(print('[% =% (myidi)mutex release(function run in threads mutex thread allocate_lock(for in range( )thread start_new_thread(counter( )make global lock object spawn threads each thread loops times time sleep( print('main thread exiting 'don' exit too early simulate real work print isn' interrupted now reallythis script simply augments example - to synchronize prints with thread lock the net effect of the additional lock calls in this script is that no two threads will parallel system tools