def counter(myidcount)for in range(count)stdoutmutex acquire(print('[% =% (myidi)stdoutmutex release(exitmutexes[myidtrue signal main thread for in range( )thread start_new_thread(counter( )while false in exitmutexespass print('main thread exiting 'the output of this script is similar to the prior-- threads counting to in parallel and synchronizing their prints along the way in factboth of the last two counting thread scripts produce roughly the same output as the original thread_count pyalbeit without stdout corruption and with larger counts and different random ordering of output lines the main difference is that the main thread exits immediately after (and no sooner than!the spawned child threadsc:\pp \system\threadspython thread-count-wait py more deleted [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = [ = main thread exiting coding alternativesbusy loopsargumentsand context managers notice how the main threads of both of the last two scripts fall into busy-wait loops at the endwhich might become significant performance drains in tight applications if sosimply add time sleep call in the wait loops to insert pause between end tests and to free up the cpu for other tasksthis call pauses the calling thread only (in this casethe main oneyou might also try experimenting with adding sleep calls to the thread function to simulate real work threads