In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the situation of python thread safety, the article is very detailed, has a certain reference value, interested friends must read it!
1. Visibility problems, visibility problems caused by CPU cache, so that the operation of a pair of shared variable A can not be immediately seen by thread 2, resulting in thread 2 getting the old value.
2. Atomic problem.
For example, the statement self._balance=self._balance-draw_amount is not an atomic CPU execution command. There are three CPU commands to execute this statement.
Example
Import threadingclass Account: # define constructor def _ _ init__ (self, account_no, balance): ": param account_no: account: param balance: balance" self.account_no = account_no self._balance = balance def draw (self Draw_amount): ": param draw_amount: money to withdraw: return:"if self._balance > draw_amount: print (threading.current_thread (). GetName () + 'successful withdrawal from' + self.account_no +" The amount withdrew from the account is: "+ str (draw_amount) +"\ n ") self._balance = self._balance-draw_amount print ('account balance is' Self._balance) else: print (threading.current_thread (). GetName () + 'from' + self.account_no + "failed to withdraw money\ n") # two threads simultaneously send and withdraw money account = Account ('account one', 2000) threading.Thread (target=account.draw, name=' thread 1', args= (800,)). Start () threading.Thread (target=account.draw, name=' thread 2', args= (800,)). Start () threading.Thread (target=account.draw) Name=' thread 3', args= (800,). Start () these are all the contents of the article "what's wrong with Python thread safety?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.