Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Multithreading
  5. Synchronizing Threads

Synchronizing Threads

Threading module is used for creating, controlling and managing threads in python program.

Syntax:-

import threading

Some common functions of the threading module in python:-

  • threading.activeCount() − Returns the number of thread objects that are active.
  • threading.currentThread() − Returns the number of thread objects in the caller’s thread control.
  • threading.enumerate() − Returns a list of all thread objects that are currently active.

 The threading module also provides many classes whose objects are very useful in creating and managing threads.

Synchronizing thread is defined as a method that assure that one or more threads are not simultaneously accessing the program segment known as critical section.

Critical section refers to the parts of the program where the shared resource is accessed.

The threading module provided by python defines the following objects for synchronizing threads:-

  • Lock – Primitive lock object
  • RLock – Re-entrant lock object allows for a single thread to acquire an already held lock.
  • Semaphore – Provides a waiting area for threads waiting for a lock
  • Timer – Similar to threads but wait an allotted period of time before running
  • Event- General version of condition variables can awaken any number of threads when the event happens.
  • Condition- Object causes one thread to wait until a certain “condition” has been satisfied by another thread.

This is all about synchronizing threads in python.

Was this article helpful to you? Yes No

How can we help?