Thread Safety

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MetalOne

    Thread Safety

    Are atomic operations thread safe in Python? Can immutable objects,
    such as, strings, be created on one thread and then accessed by
    another thread, without using locks?

    Such operations are not guaranteed thread safe in Java due to caching
    optimizations.

    If someone could point me to reference documentation on this, that
    would be great.
  • Martin v. Löwis

    #2
    Re: Thread Safety

    MetalOne wrote:[color=blue]
    > Are atomic operations thread safe in Python?[/color]

    I don't understand the question. Atomic operations, by nature, can
    either completely succeed or completely fail - otherwise it would not
    be atomic. This is independent of Python.

    In general, you cannot cause inconsistency of Python's internal
    representation of objects by pure Python code executed in multiple
    threads, so Python itself is thread-safe.
    [color=blue]
    > Can immutable objects,
    > such as, strings, be created on one thread and then accessed by
    > another thread, without using locks?[/color]

    Yes.
    [color=blue]
    > Such operations are not guaranteed thread safe in Java due to caching
    > optimizations.[/color]

    But then, in Java, strings are not immutable, no?

    Regards,
    Martin

    Comment

    • Alex Martelli

      #3
      Re: Thread Safety

      "Martin v. Löwis" wrote:
      ...[color=blue]
      > But then, in Java, strings are not immutable, no?[/color]

      Nope: strings ARE immutable in Java .


      Alex

      Comment

      • MetalOne

        #4
        Re: Thread Safety

        "Martin v. Löwis" <martin@v.loewi s.de> wrote in message news:<bli0p3$p2 3$07$1@news.t-online.com>...[color=blue]
        > MetalOne wrote:[color=green]
        > > Are atomic operations thread safe in Python?[/color]
        >
        > I don't understand the question. Atomic operations, by nature, can
        > either completely succeed or completely fail - otherwise it would not
        > be atomic. This is independent of Python.
        >
        > But then, in Java, strings are not immutable, no?
        >
        > Regards,
        > Martin[/color]

        The problem in Java is one of visibility. Assigning
        s = "hello"
        does not necessarily happen if < s > is not used in the current
        thread.

        Likewise, another thread may see no reason to examine < s >, if < s >
        has not been changed in this thread.

        I found the following on a Java site.
        # the language definition ensures that single memory moves concerning
        32-bit values (int, etc.) are "atomic" (not interrupted)
        # moving a 64-bit value (say, long) can be preempted in the middle of
        the transfer operation (but it may not!)

        # however, the atomicity of 32-bit moves ensures nothing about the
        visibility of updated (lastly written) values (see below)

        # the atomicity and correct visibility of a scalar (32- or 64-bit)
        variable can be ensured by defining it volatile
        # a volatile variable is always kept up-to-date and atomically loaded
        from and stored into the main memory
        # unfortunately, not all current Java VM implementations handle
        volatile variables correctly


        Java strings are immutable. There is a StringBuffer class that is
        mutable.

        Comment

        • Martin v. Löwis

          #5
          Re: Thread Safety

          jcb@iteris.com (MetalOne) writes:
          [color=blue]
          > The problem in Java is one of visibility. Assigning
          > s = "hello"
          > does not necessarily happen if < s > is not used in the current
          > thread.[/color]

          I see. Python does guarantee that assignments are visible in other
          threads once they have completed. It also guaranteed that the thread
          either sees the old or the new value, i.e. assignments are atomic
          (unless an object implements __setitem__).

          Regards,
          Martin

          Comment

          • ykingma@accessforall.nl

            #6
            Re: Thread Safety

            "Martin v. Löwis" wrote:
            [color=blue]
            > MetalOne wrote:[color=green]
            >> Are atomic operations thread safe in Python?[/color]
            >
            > I don't understand the question. Atomic operations, by nature, can
            > either completely succeed or completely fail - otherwise it would not
            > be atomic. This is independent of Python.
            >
            > In general, you cannot cause inconsistency of Python's internal
            > representation of objects by pure Python code executed in multiple
            > threads, so Python itself is thread-safe.[/color]

            For the namespaces in Jython this 'Python internal thread safety'
            is handled by the Java class:



            which has almost all of it public methods Java synchronized:

            Download Jython for free. Jython is a Java implementation of the Python language. It allows users to compile Python source code to Java byte codes, and run the resulting bytecodes on any Java Virtual Machine.


            Thinking about it: is this the Jython parallel of CPython's
            Global Interpreter Lock, the famous GIL?

            Regards,
            Ype

            --
            email at xs4all.nl

            Comment

            Working...