Hello to everyone
I wont to create 2 threads in Python ( thread01, thread02 ), which doing the follows :
thread01, ask user to give a value to flag variable and write it to the shared memory of two threads
thread02, read from shared memory and check if the flag have a specific value .
The corresponding python code follows:
[CODE=python]
import time
import thread
import threading
import Numeric, sys
class newThread(threa ding.Thread):
def __init__(self,n ame,threadID,sl eeptime):
self.name=name
self.threadID=t hreadID
self.sleeptime= sleeptime
def run(self,flag):
print time.ctime(time .time())
if self.threadID== 2:
print "%s flag : "%(self.name,fl ag)
while flag!=100:
time.sleep(self .sleeptime)
sys.exit()
elif self.threadID== 1:
while 1:
print "%s flag:"%(self.na me, flag)
flag=int(raw_in put("Give a value at flag: "))
time.sleep(self .sleeptime)
flag=0
thread1=newThre ad("Thread No:1",1,5)
thread2=newThre ad("Thread No:2",2,15)
thread1.run(fla g)
thread2.run(fla g)
while 1:
pass
thread1.exit()
thread2.exit()[/CODE]
When I call it from python 2.5(Linux kubuntu 7.04), thread02 never take the CPU and only thread01 execute. Why does this happen ? The 2 threads does not run concurrently in double core processors?Why does not thread02 take cpu when thread 1 sleep? the Is there any solution of this problem?
I wont to create 2 threads in Python ( thread01, thread02 ), which doing the follows :
thread01, ask user to give a value to flag variable and write it to the shared memory of two threads
thread02, read from shared memory and check if the flag have a specific value .
The corresponding python code follows:
[CODE=python]
import time
import thread
import threading
import Numeric, sys
class newThread(threa ding.Thread):
def __init__(self,n ame,threadID,sl eeptime):
self.name=name
self.threadID=t hreadID
self.sleeptime= sleeptime
def run(self,flag):
print time.ctime(time .time())
if self.threadID== 2:
print "%s flag : "%(self.name,fl ag)
while flag!=100:
time.sleep(self .sleeptime)
sys.exit()
elif self.threadID== 1:
while 1:
print "%s flag:"%(self.na me, flag)
flag=int(raw_in put("Give a value at flag: "))
time.sleep(self .sleeptime)
flag=0
thread1=newThre ad("Thread No:1",1,5)
thread2=newThre ad("Thread No:2",2,15)
thread1.run(fla g)
thread2.run(fla g)
while 1:
pass
thread1.exit()
thread2.exit()[/CODE]
When I call it from python 2.5(Linux kubuntu 7.04), thread02 never take the CPU and only thread01 execute. Why does this happen ? The 2 threads does not run concurrently in double core processors?Why does not thread02 take cpu when thread 1 sleep? the Is there any solution of this problem?
Comment