title: How to create a lock for a specific variable in main while using queues in threads
body: <p>I am trying to figure out how in python one would declare a specific variable to be locked so that only one thread may access it at a time to avoid race conditions. If I have two threads constantly updating a variable via queues, but I am also updating the variable manually in main, what would be the right way to declare that variable a shared resource by all threads so that only one may access it at a time between the threads being run and main?</p>
  
  <p>I wrote a little example code to show what I mean.</p>
  
  <pre><code>import time
  
  from random import randint
  from threading import Thread
  from queue import Queue
  
  # Add the amount by random number from 1 - 3 every second
  def producer(queue, amount):
      while True:
          time.sleep(1)
          amount[0] += randint(1, 3)
          queue.put(amount)
  
  # Subtract the amount by random number from 1 - 3 every second
  def consumer(queue, amount):
      while True:
          item = queue.get()
          amount[0] -= randint(1, 3)
          queue.task_done()
  
  amount = [10]
  
  queue = Queue()
  t1 = Thread(target=producer, args=(queue, amount,))
  t2 = Thread(target=consumer, args=(queue, amount,))
  
  t1.start()
  t2.start()
  
  while True:
      n = input("Type a number or q: ")
      if n == 'q':
          break
      else:
          # Here is where I am confused about how to declare the amount a
          # shared resource and lock it in a way that the queues would also 
          # adhere to
          amount[0] += int(n)
          print("amount is now: {}".format(amount[0]))
  
  t1.join()
  t2.join()
  </code></pre>