Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what the function of "flag" in example2 how_to_determine_if_a_thread_has_started? #26

Open
wwang-chcn opened this issue Dec 25, 2019 · 0 comments

Comments

@wwang-chcn
Copy link

I don't understand the function of flag in example2 how_to_determine_if_a_thread_has_started (12.2). The scripts works fine when I remove the flag part likes below.

import threading
import time
class PeriodicTimer:
    def __init__(self, interval):
        self._interval = interval
        self._cv = threading.Condition()
    
    def start(self):
        t = threading.Thread(target=self.run)
        t.daemon = True
        t.start()
    
    def run(self):
        print('running start')
        while True:
            time.sleep(self._interval)
            with self._cv:
                self._cv.notify_all()
    
    def wait_fot_tick(self):
        with self._cv:
            self._cv.wait()


ptimer = PeriodicTimer(5)
ptimer.start()

def countdown(nticks):
    print('countdown start')
    while nticks > 0:
        ptimer.wait_fot_tick()
        print('T-minus', nticks)
        nticks -= 1


def countup(last):
    n = 0
    print('counting start')
    while n < last:
        ptimer.wait_fot_tick()
        print('Counting', n)
        n += 1


threading.Thread(target=countdown, args=(10,)).start()
threading.Thread(target=countup, args=(5,)).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant