June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,49 @@
|
|||
(ql:quickload '(:stmx :bordeaux-threads))
|
||||
|
||||
(defpackage :dining-philosophers
|
||||
(:use :cl))
|
||||
|
||||
(in-package :dining-philosophers)
|
||||
|
||||
(defstruct philosopher
|
||||
name
|
||||
left-fork
|
||||
right-fork)
|
||||
|
||||
(defparameter *philosophers* '("Aristotle" "Kant" "Spinoza" "Marx" "Russell"))
|
||||
(defparameter *eating-max* 5.0)
|
||||
(defparameter *thinking-max* 5.0)
|
||||
(defvar *log-lock* (bt:make-lock))
|
||||
(defvar *running* nil)
|
||||
|
||||
(defun print-log (name status)
|
||||
(bt:with-lock-held (*log-lock*)
|
||||
(format t "~a is ~a~%" name status)))
|
||||
|
||||
(defun philosopher-cycle (philosopher)
|
||||
"Continously atomically grab and return the left and right forks of the given PHILOSOPHER."
|
||||
(with-slots (name left-fork right-fork) philosopher
|
||||
(loop while *running*
|
||||
do
|
||||
(print-log name "hungry")
|
||||
(stmx:atomic
|
||||
(stmx.util:take left-fork)
|
||||
(stmx.util:take right-fork))
|
||||
(print-log name "eating")
|
||||
(sleep (random *eating-max*))
|
||||
(stmx:atomic
|
||||
(stmx.util:put left-fork t)
|
||||
(stmx.util:put right-fork t))
|
||||
(print-log name "thinking")
|
||||
(sleep (random *thinking-max*)))))
|
||||
|
||||
(defun scenario ()
|
||||
(let ((forks (loop repeat (length *philosophers*) collect (stmx.util:tcell t))))
|
||||
(setf *running* t)
|
||||
(loop for name in *philosophers*
|
||||
for left-fork in forks
|
||||
for right-fork in (append (cdr forks) (list (car forks)))
|
||||
do (let ((philosopher (make-philosopher :name name :left-fork left-fork :right-fork right-fork)))
|
||||
(bt:make-thread (lambda () (philosopher-cycle philosopher))
|
||||
:initial-bindings (cons (cons '*standard-output* *standard-output*)
|
||||
bt:*default-special-bindings*))))))
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
DINING-PHILOSOPHERS> (scenario)
|
||||
Aristotle is hungry
|
||||
Aristotle is eating
|
||||
Kant is hungry
|
||||
Spinoza is hungry
|
||||
Spinoza is eating
|
||||
Marx is hungry
|
||||
NIL
|
||||
Russell is hungry
|
||||
Aristotle is thinking
|
||||
Russell is eating
|
||||
Spinoza is thinking
|
||||
Kant is eating
|
||||
Spinoza is hungry
|
||||
Russell is thinking
|
||||
Marx is eating
|
||||
Kant is thinking
|
||||
Aristotle is hungry
|
||||
Aristotle is eating
|
||||
Marx is thinking
|
||||
Spinoza is eating
|
||||
Spinoza is thinking
|
||||
Marx is hungry
|
||||
Marx is eating
|
||||
Russell is hungry
|
||||
Marx is thinking
|
||||
Kant is hungry
|
||||
Aristotle is thinking
|
||||
Russell is eating
|
||||
Kant is eating
|
||||
Marx is hungry
|
||||
Spinoza is hungry
|
||||
Kant is thinking
|
||||
Spinoza is eating
|
||||
Kant is hungry
|
||||
Aristotle is hungry
|
||||
Russell is thinking
|
||||
Aristotle is eating
|
||||
Aristotle is thinking
|
||||
Aristotle is hungry
|
||||
Aristotle is eating
|
||||
Spinoza is thinking
|
||||
Marx is eating
|
||||
...
|
||||
Loading…
Add table
Add a link
Reference in a new issue