Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 730579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:55:42+00:00 2026-05-14T06:55:42+00:00

With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe I wanted to know if the following:

  • 0

With reference to the following link: http://docs.python.org/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe

I wanted to know if the following:

(x, y) = (y, x)

will be guaranteed atomic in cPython. (x and y are both python variables)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:55:43+00:00Added an answer on May 14, 2026 at 6:55 am

    Let’s see:

    >>> x = 1
    >>> y = 2
    >>> def swap_xy():
    ...   global x, y
    ...   (x, y) = (y, x)
    ... 
    >>> dis.dis(swap_xy)
      3           0 LOAD_GLOBAL              0 (y)
                  3 LOAD_GLOBAL              1 (x)
                  6 ROT_TWO             
                  7 STORE_GLOBAL             1 (x)
                 10 STORE_GLOBAL             0 (y)
                 13 LOAD_CONST               0 (None)
                 16 RETURN_VALUE    
    

    It doesn’t appear that they’re atomic: the values of x and y could be changed by another thread between the LOAD_GLOBAL bytecodes, before or after the ROT_TWO, and between the STORE_GLOBAL bytecodes.

    If you want to swap two variables atomically, you’ll need a lock or a mutex.

    For those desiring empirical proof:

    >>> def swap_xy_repeatedly():
    ...   while 1:
    ...     swap_xy()
    ...     if x == y:
    ...       # If all swaps are atomic, there will never be a time when x == y.
    ...       # (of course, this depends on "if x == y" being atomic, which it isn't;
    ...       #  but if "if x == y" isn't atomic, what hope have we for the more complex
    ...       #  "x, y = y, x"?)
    ...       print 'non-atomic swap detected'
    ...       break
    ... 
    >>> t1 = threading.Thread(target=swap_xy_repeatedly)
    >>> t2 = threading.Thread(target=swap_xy_repeatedly)
    >>> t1.start()
    >>> t2.start()
    >>> non-atomic swap detected
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following the spring-security documentation: http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ldap.html I am trying to set up ldap authentication (very
I have the following link: http://www.europarl.europa.eu/sides/getDoc.do?type=REPORT&mode=XML&reference=A7-2010-0001&language=EN the reference part of the url has the
I'm using the time picker control from the following link: http://fgelinas.com/code/timepicker/timepicker.html It works super
I'm using the time picker control from the following link: http://fgelinas.com/code/timepicker/timepicker.html It works super
I am trying to run PhotoStream sample from following link http://android-developers.blogspot.com/2008/09/android-photostream.html But when i
i can reference link with specific id with following code $(document).ready(function(){ $(a#example_link_id).click(function(event){ var url
i have the following references to jquery on my page: <link href=http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css rel=stylesheet type=text/css/>
The following code raises an ambiguous reference to value at compile time: import flash.display.Sprite;
http://developers.facebook.com/docs/reference/api/post/ doesn't seem to give a clue about it. Would Install(ed) be an action?
I am using the code listed on the following link: http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/programmatically_adding_attributes_and_attribute_sets Everything works until

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.