Multithreading and locking

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

    Multithreading and locking

    Is it true what I heard (as a "rumour" of sorts), that in multithreaded
    Python programs global variables are already automagically protected by
    mutexes? Can someone clarify on that?

    --
    --
    Every sufficiently advanced magic is indistinguishab le from technology
    - Arthur C Anticlarke


  • Duncan Booth

    #2
    Re: Multithreading and locking

    "Ivan Voras" <ivoras@fer.h r> wrote in news:bkbv5l$kgl $1@bagan.srce.h r:
    [color=blue]
    >
    > Is it true what I heard (as a "rumour" of sorts), that in multithreaded
    > Python programs global variables are already automagically protected by
    > mutexes? Can someone clarify on that?
    >[/color]
    The Python interpreter holds a global mutex which is released by some
    methods that call the OS, and is also released sometimes between bytecode
    instructions.

    The effect of this is that any operation that takes a single bytecode
    instruction, and which does not call the OS, cannot be interrupted by
    another Python thread. It is left to the user to work out when this means a
    multithreaded operation will be safe.

    For example:

    x.extend(y)

    If x is a list, then this is safe in a multithreaded environment. If x is a
    user defined class and the extend method is coded in Python, then this
    probably isn't safe.

    x += y

    If x and y are lists, this appears superficially to be the same as the
    first example, however this code compiles to two bytecode instructions (an
    inplace add and a store), so it could be interrupted with unfortunate
    consequences.

    The bottom line is that with care you can use a list for multi-threaded
    queue operations, but with a Queue class already there and waiting it isn't
    usually worth the risk.

    --
    Duncan Booth duncan@rcp.co.u k
    int month(char *p){return(1248 64/((p[0]+p[1]-p[2]&0x1f)+1)%12 )["\5\x8\3"
    "\6\7\xb\1\x9\x a\2\0\4"];} // Who said my code was obscure?

    Comment

    • Aahz

      #3
      Re: Multithreading and locking

      In article <bkbv5l$kgl$1@b agan.srce.hr>, Ivan Voras <ivoras@fer.h r> wrote:[color=blue]
      >
      >Is it true what I heard (as a "rumour" of sorts), that in multithreaded
      >Python programs global variables are already automagically protected by
      >mutexes? Can someone clarify on that?[/color]

      In addition to Duncan's excellent answer, I'd suggest that if you're
      using phrases like "global variables", you probably want to learn more
      about how Python's object model works in addition to learning more about
      writing good threaded programs. You want to avoid sharing mutable names
      across threads as much as possible.
      --
      Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

      "It is easier to optimize correct code than to correct optimized code."
      --Bill Harlan

      Comment

      Working...