title: Python threading. How do I lock a thread?
body: <p>I'm trying to understand the basics of threading and concurrency. I want a simple case where two threads repeatedly try to access one shared resource.</p>
  <p>The code:</p>
  <pre><code>import threading
  
  class Thread(threading.Thread):
      def __init__(self, t, *args):
          threading.Thread.__init__(self, target=t, args=args)
          self.start()
  count = 0
  lock = threading.Lock()
  
  def increment():
      global count 
      lock.acquire()
      try:
          count += 1    
      finally:
          lock.release()
     
  def bye():
      while True:
          increment()
          
  def hello_there():
      while True:
          increment()
  
  def main():    
      hello = Thread(hello_there)
      goodbye = Thread(bye)
      
      while True:
          print count
  
  if __name__ == '__main__':
      main()
  </code></pre>
  <p>So, I have two threads, both trying to increment the counter. I thought that if thread 'A' called <code>increment()</code>, the <code>lock</code> would be established, preventing 'B' from accessing until 'A' has released.</p>
  <p>Running the makes it clear that this is not the case. You get all of the random data race-ish increments.</p>
  <p>How exactly is the lock object used?</p>
  <p>Additionally, I've tried putting the locks inside of the thread functions, but still no luck.</p>