In a python program I need 2 threads based on Threading module. Thread#1 generates some data and put it in a buffer and thread#2 is supposed to process the data in the buffer.
So my pseudocode is like this: Thread 1:
Thread#1
while True:
Generate_Some_Data()
while flag==1:
pass()
Buffer_Address=Write_It_To_Buffer()
flag=1
Thread#2
while True:
while flag==0:
pass()
Process_Data(Buffer_Address)
flag=0
(Let us assume that access to the variable "flag" is atomized with suitable locks.)
I know that Threading module is not concurrent. Essentially, this means that unless one of the threads does not block on some external condition (such as file-io or time.sleep), both threads will share the total process time regardless of the tasks. Therefore, according to my understanding, approximately half of the total processing time will be wasted on "while flag" loops in the above configuration.
So, here are my questions:
Am I right in my above anticipation/understanding of the Threading module? Is the half of the total process time wasted in "while flag" loops?
(If I am right) Is there anyway to get rid of "while flag" loops completely? I tried to find out another structure in which I can make threads sleep in lock.acquire() methods, however, I could not figure out a %100 safe way of doing so. (When one thread relases lock, there is no quarantee that the other thread will acquire it before the same thread acquire it again)