The atomic nature of the program, Python to achieve atomicity, lock unlock Threading. Lock ()

Source: Internet
Author: User
Tags mutex

A want to transfer 1000 dollars from your account to B's account. The process of transferring money from a to the end of a transfer is called a transaction. In this transaction, do the following:

1. Subtract 1000 dollars from the account of a. If A's account originally had 3000 dollars, it would now be 2000 dollars.

2. Add 1000 dollars to the account in B. If B's account had 2000 dollars, it would now be 3000 dollars.

If the account of a has been reduced by 1000 dollars, suddenly an accident, such as a power outage or something, causing the transfer transaction unexpectedly terminated, and at this time B's account has not increased by 1000 yuan. Well, we call this operation a failure, to roll back. Rollback is to return to the state before the start of the transaction, that is, back to a account has not been reduced by 1000 blocks of state, B's account of the original state. At this point A's account still has 3000 pieces, B's account still has 2000 blocks.

We have succeeded in either together (a account successfully reduced by 1000, while the B account was successfully increased by 1000), or the operation that failed together (a account returned to its original state and B account back to its original state) was called an atomic operation.

If a transaction can be thought of as a program, it is either completely executed or not executed at all. This property is called atomicity.

The use of [mutex mutex] in Python prevents multiple threads from simultaneously modifying a variable caused by an error.

Mutex = Threading. Lock () Creates a lock that is not locked by default

Mutex.acquire () lock

Mutex.release () Unlock

If a thread executes to mutex.acquire (), if the lock is found to be locked then stop execution until the other thread performs the unlock Operation Mutex.release ()

The principle of locking is that the line of code is as small as possible

The atomic nature of the program, Python to achieve atomicity, lock unlock Threading. Lock ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.