<commit_before>;;;; An example of using condition variable broadcast to wake other threads.
(import (scheme base)
        (scheme read)
        (scheme write)
        (srfi 18))

(define cv (make-condition-variable))
(define m (make-mutex))

;; Thread - Sleep, then wake other threads up via broadcast
(thread-start!
  (make-thread
    (lambda ()
      (write "started thread")
      (thread-sleep! 3000)
      (condition-variable-broadcast! cv)
      (write "thread done"))))

;; Thread - wait for broadcast
(thread-start!
  (make-thread
    (lambda ()
      (write "started waiting thread")
      (mutex-lock! m)
      (write "register waiting thread cv")
      (mutex-unlock! m cv)
      (write "waiting thread done"))))

;; Main thread - wait for broadcast
(mutex-lock! m)
(mutex-unlock! m cv) ;; Wait on cv
(write "main thread done")
(thread-sleep! 500)
<commit_msg>Proper use of condition variable<commit_after>;;;; An example of using a condition variable to wake other threads.
(import (scheme base)
        (scheme read)
        (scheme write)
        (srfi 18))

(define *done* #f)
(define cv (make-condition-variable))
(define m (make-mutex))

(define (trace msg)
  (display msg)
  (newline))

;; Thread - Sleep, then wake other threads up via broadcast
(thread-start!
  (make-thread
    (lambda ()
      (thread-sleep! 3000)
      (set! *done* #t)
      (condition-variable-broadcast! cv)
      (trace "broadcast thread done"))))

;; Thread - wait for broadcast
(thread-start!
  (make-thread
    (lambda ()
      (let loop ()
        (mutex-lock! m)
        (cond
          (*done*
            (trace "waiting thread done")
            (mutex-unlock! m))
          (else
            (trace "waiting thread - waiting for cv")
            (mutex-unlock! m cv)
            (loop)))))))

;; Main thread - wait for broadcast
(let loop ()
  (mutex-lock! m)
  (cond
    (*done*
      (mutex-unlock! m)
      (thread-sleep! 500)
      (trace "main thread done"))
    (else
      (mutex-unlock! m cv) ;; Wait on cv
      (loop))))

{'fraction_non_alphanumeric': 0.1376597836774828, 'fraction_numerical': 0.008849557522123894, 'mean_word_length': 2.490566037735849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1990810', 'n_tokens_mistral': 643, 'n_tokens_neox': 618, 'n_words': 213}