マルチスレッドの基本的な概念しか知らないのですが、現在、助けが必要な状況に遭遇しています。
完了するタスクが 2 つあり、両方を継続的に実行する必要があります。問題は、最初のスレッドが最初にいくつかのジョブを実行した後にのみ、2 番目のタスクを開始する必要があるということです。現在、2 つのスレッド クラスは大まかに次のようになっています。
finished = False # shared flag
class first(threading.Thread):
def __init__(self, cond, finished):
threading.Thread.__init__(self)
self.cond = cond
self.finished = finished
def run(self):
self.cond.aquire()
do_something()
self.finished = True #change the flag
self.cond.notify()
self.cond.release()
do_something_else()
class second(threading.Thread):
def __init__(self, cond, finished):
threading.Thread.__init__(self)
self.cond = cond
self.finished = finished
def run(self):
self.cond.aquire()
while self.finished == False:
self.cond.wait()
self.cond.release()
do_something()
ただし、実際には、wait() と notify() に関係なく、プログラムは依然としてランダムに実行されます。誰でもこの問題で私を助けることができますか? ありがとう。