Search code examples
pythonpython-multithreadingcondition-variablecritical-section

Multithreaded socket Program - Handling Critical section


I am creating a multi-threaded program, in which I want only 1 thread at a time to go in the critical section where is creates a socket and send some data and all the other to wait for that variable to clear.

  1. I tried threading.Events but later realized that on set() it will notify all the threads waiting. While I only wanted to notify one.
  2. Tried locks(acquire and release). It suited my scenario well but I got to know that lock contention for a long time is expensive. After acquiring the lock my thread was performing many functions and hence resulted in holding the lock for long.
  3. Now I tried threading.conditions. Just wanted to know if acquiring and holding the condition for a long time, is it not a good practice as it also uses locks. Can anyone suggest a better approach to my problem statement.

Solution

  • I would use an additional thread dedicated to sending. Use a Queue where the other threads put their Send-Data. The socket-thread gets items from the queue in a loop and sends them one after the other.

    As long as the queue is empty, .get blocks and the send-thread sleeps.

    The "producer" threads have no waiting time at all, they just put their data in the queue and continue.

    There is no concern about possible deadlock conditions.

    To stop the send-thread, put some special item (e.g. None) in the queue.

    To enable returning of values, put a tuple (send_data, return_queue) in the send-queue. when a result is ready, return it by putting it in the return_queue.