Python runs only one thread if a condition inside a thread contains global variable. Is there a way to fix this?

174 views Asked by At

I have two threads, both of which must run in parallel. But only the first one runs properly, and since both the threads are dependent on each other, so the first thread keeps on running.

I cant paste the whole code here, but here's the part which is causing the problem,

def startsending():
       while(starting_index<=ending_index):
              #some statements
              clientsocket.sendto(data[index],(serverIP,serverPort))
              #some statements

def startreceiving():
       while(starting_index<=ending_index):
              ack,servAddr = clientSocket.recvfrom(1050)
              ackwindow[ack-starting_index] = 1
              starting_index+=1
              #some statements

Here, starting_index, ending_index and ackwindow are global variables, and only the thread startsending() works if the "global" is used. I've also tried using locks, but if locks are used, the whole while loop will have to be inside the critical section.

Actually, the while loops are the threads themselves here. Both the loops must run simultaneously for the code to work.

Is there a way to fix this problem? Or do i have to do it without threads, in a sequential manner?

0

There are 0 answers