multithreading - python threading.lock() not working -


i created function modify global variable, adds 1 each time. create 10 threads call function; each thread call function 10 times. however, variable's final value not same if call function 100 times in 1 thread. wrong how use threading.lock? below code:

num=0 lockone=threading.lock() def subpro():     global num     lockone.acquire()     num+=1     lockone.release() 

you not waiting threads complete. have use thread.join:

import threading  num=0 lockone=threading.lock() def subpro():     global num     lockone.acquire()     num+=1     lockone.release()  def run():     in range(10):         subpro()  # start threads threads = [threading.thread(target=run) x in range(10)] thread in threads:     thread.start() # wait completion thread in threads:     thread.join() print(num) 

Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

android - How to create dynamically Fragment pager adapter -

1111. appearing after print sequence - php -