Race condition disappears when compiled? added by jacius on Fri May 14 09:03:34 2021

;; This code has a race condition. When run in csi, it prints some
;; number less than 20000. But when compiled, it always prints 20000.
;; What causes the race condition to disappear when compiled?

  (use srfi-18))
  (import (srfi 18))))

(define iterations 10000)

(define x 0)

(define (make-counter)
  (lambda ()
    (do ((i 0 (add1 i)))
        ((= i iterations))
      (set! x (add1 x)))))

(define thread1 (thread-start! (make-counter)))
(define thread2 (thread-start! (make-counter)))

(thread-join! thread1)
(thread-join! thread2)

(print x)