The Computer Language
Benchmarks Game

thread-ring Racket program

source code

#lang racket/base

;;; The Computer Language Benchmarks Game
;;; http://benchmarksgame.alioth.debian.org/

;;; contributed by Matthew Flatt

;; Uses Racket threads

(require racket/cmdline)

;; Each thread runs this loop:
(define (run id next)
  (let ([v (thread-receive)])
    (cond
     [(zero? v) ;; Done
      (printf "~a\n" id)
      (exit)]
     [else ;; Keep going
      (thread-send next (sub1 v))
      (run id next)])))
                       

(let ([n (command-line #:args (n) (string->number n))])
  ;; The original thread is #503. Create the rest:
  (let ([t1 (for/fold ([next (current-thread)])
                      ([id (in-range 502 0 -1)])
              (thread (lambda () (run id next))))])
    ;; Start:
    (thread-send t1 n)
    (run 503 t1)))
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Welcome to Racket v6.6.


Sat, 23 Jul 2016 21:09:07 GMT

MAKE:
/usr/local/src/racket-6.6/bin/raco make threadring.racket
0.58s to complete and log all make actions

COMMAND LINE:
/usr/local/src/racket-6.6/bin/racket ./compiled/threadring_racket.zo 50000000

PROGRAM OUTPUT:
292