Threads: does Thread.start() atomically set Thread.__started ?

Discussion in 'Python' started by Enigma Curry, Mar 14, 2006.

  1. Enigma Curry

    Enigma Curry Guest

    Can some kind person please further my education on Threads?

    When I create a thread called "t" and I do a "t.start()" am I
    guaranteed that "t.isAlive()" will return True as long as the thread
    hasn't already completed? Put another way, does "t.start()" ever return
    before t.__started is set to True?

    consider this example:

    import time
    import threading
    class MyThread(threading.Thread):
    def __init__(self):
    self.completed = False
    threading.Thread.__init__(self)
    def run(self):
    #do something
    time.sleep(1)
    self.completed = True

    t = MyThread()
    while t.isAlive() == False and t.completed == False:
    t.start()

    In the above code, am I guaranteed that t will only be (attempted to
    be) started once?


    Thanks,
    Ryan
    Enigma Curry, Mar 14, 2006
    #1
    1. Advertising

  2. Enigma Curry

    Peter Hansen Guest

    Re: Threads: does Thread.start() atomically set Thread.__started?

    Enigma Curry wrote:
    > Can some kind person please further my education on Threads?
    >
    > When I create a thread called "t" and I do a "t.start()" am I
    > guaranteed that "t.isAlive()" will return True as long as the thread
    > hasn't already completed? Put another way, does "t.start()" ever return
    > before t.__started is set to True?


    Did you check the source? It's fairly straightforward, as I recall.
    (threading.py in the python library)

    -Peter
    Peter Hansen, Mar 15, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Davis
    Replies:
    0
    Views:
    822
    Jon Davis
    Nov 14, 2006
  2. Chris Thomasson
    Replies:
    42
    Views:
    1,970
    Casper H.S. Dik
    Sep 22, 2007
  3. news.rcn.com
    Replies:
    2
    Views:
    1,104
    Roedy Green
    Dec 10, 2007
  4. Chris Thomasson
    Replies:
    35
    Views:
    2,132
    Chris M. Thomasson
    Aug 1, 2008
  5. wrex
    Replies:
    2
    Views:
    142
Loading...

Share This Page