(Lispex)sicp.io
3.9 · 공유 상태의 엇갈린 실행

같은 갱신도 읽기와 쓰기가 엇갈리면 결과가 달라질 수 있다.

각 읽기와 쓰기를 별도 단계로 드러내면 유한한 실행 순서 하나에서 갱신 손실을 직접 볼 수 있습니다.

생각해 볼 질문

두 갱신이 어느 쪽도 끝나기 전에 같은 잔액을 읽으면 무엇이 사라질까요?

  • Separate each transaction read from its later write
  • Recognize a write computed from a stale shared value
  • Enumerate the exact step order of a finite schedule
  • Compare a lost-update result with a serialized result

The deposit adds 10 and the withdrawal subtracts 20. In the first schedule, both operations read 100. The deposit writes 110, but the withdrawal still computes from its earlier read and writes 80. The later write replaces the deposit result, so the final balance does not contain both changes.

The second schedule lets the deposit read and write before the withdrawal reads. The withdrawal therefore sees 110 and writes 90. These programs enumerate two already chosen finite schedules. They do not create threads or a serializer, and they do not establish the result of every possible interleaving.

리스펙스 · SICP 코드SICP에 필요한 Scheme 호환 문법을 리스펙스 SICP 프로필로 실행합니다.
(begin
  (define balance 100)
  (define deposit-read balance)
  (define withdraw-read balance)
  (define deposit-write (+ deposit-read 10))
  (set! balance deposit-write)
  (define withdraw-write (- withdraw-read 20))
  (set! balance withdraw-write)
  (list (list 'deposit 'read deposit-read)
        (list 'withdraw 'read withdraw-read)
        (list 'deposit 'write deposit-write)
        (list 'withdraw 'write withdraw-write)
        (list 'final balance)))
리스펙스 학습용 런타임리스펙스 SICP 프로필 1.0.0
리스펙스 SICP 런타임 불러오는 중
리스펙스 · SICP 코드UTF-8 461 / 1,048,576바이트
예제
결과
출력
진단
보이는 실행 흐름0 / 0 개의 실행 이벤트
    이 브라우저 결과는 리스펙스 바우치나 권한이 아닙니다.wasm —
    예상 관찰

    The first program returns ((deposit read 100) (withdraw read 100) (deposit write 110) (withdraw write 80) (final 80)). The serialized program returns ((deposit read 100) (deposit write 110) (withdraw read 110) (withdraw write 90) (final 90)).

    실행 흐름에서 볼 점

    Follow the two initial balance reads before either assignment in the first run, then locate the writes of 110 and 80. In the second run, the withdrawal read follows the write of 110. The bounded traces describe only these two explicit schedules.

    직접 해보기

    힌트를 보기 전에 프로그램을 바꿔 보세요.

    In the first schedule, place the withdrawal write before the deposit write. Predict the final balance and name the update that is then lost.

    힌트 하나 보기

    Both writes were computed from 100, so the write that occurs last determines the final balance.