Alex Rivera | Logout

Python Threads - Critical Section

Asked 2009-01-07T03:58:18.180
17

What is the "critical section" of a thread (in Python)?

A thread enters the critical section by calling the acquire() method, which can either be blocking or non-blocking. A thread exits the critical section, by calling the release() method.

- Understanding Threading in Python, Linux Gazette

Also, what is the purpose of a lock?

Edit
Report

2 Answers

25

Other people have given very nice definitions. Here's the classic example:

import threading
account_balance = 0 # The "resource" that zenazn mentions.
account_balance_lock = threading.Lock()

def change_account_balance(delta):
    global account_balance
    with account_balance_lock:
        # Critical section is within this block.
        account_balance += delta

Let's say that the += operator consists of three subcomponents:

  • Read the current value
  • Add the RHS to that value
  • Write the accumulated value back to the LHS (technically bind it in Python terms)

If you don't have the with account_balance_lock statement and you execute two change_account_balance calls in parallel you can end up interleaving the three subcomponent operations in a hazardous manner. Let's say you simultaneously call change_account_balance(100) (AKA pos) and change_account_balance(-100) (AKA neg). This could happen:

pos = threading.Thread(target=change_account_balance, args=[100])
neg = threading.Thread(target=change_account_balance, args=[-100])
pos.start(), neg.start()
  • pos: read current value -> 0
  • neg: read current value -> 0
  • pos: add current value to read value -> 100
  • neg: add current value to read value -> -100
  • pos: write current value -> account_balance = 100
  • neg: write current value -> account_balance = -100

Because you didn't force the operations to happen in discrete chunks you can have three possible outcomes (-100, 0, 100).

The with [lock] statement is a single, indivisible operation that says, "Let me be the only thread executing this block of code. If something else is executing, it's cool -- I'll wait." This ensures that the updates to the account_balance are "thread-safe" (parallelism-safe).

answered 2009-01-07T04:42:43.037
0

A "critical section" is a chunk of code in which, for correctness, it is necessary to ensure that only one thread of control can be in that section at a time. In general, you need a critical section to contain references that write values into memory that can be shared among more than one concurrent process.

answered 2009-01-07T04:09:57.347

Your Answer