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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Enigma Curry

    #1

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

    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(thread ing.Thread):
    def __init__(self):
    self.completed = False
    threading.Threa d.__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

  • Peter Hansen

    #2
    Re: Threads: does Thread.start() atomically set Thread.__starte d?

    Enigma Curry wrote:[color=blue]
    > 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?[/color]

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

    -Peter

    Comment

    • Enigma Curry

      #3
      Re: Threads: does Thread.start() atomically set Thread.__starte d ?

      Peter,

      Thanks for the reference! I don't know why but for some reason I
      thought that I would be wading through a bunch of C code (which I know
      very little of). I haven't found my answer yet but this threading.py
      does look fairly straightforward .

      Thanks!

      Comment

      Working...