Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -1,20 +1,19 @@
|
|||
import std.stdio, std.algorithm, std.string, std.parallelism,
|
||||
core.sync.mutex;
|
||||
core.sync.mutex;
|
||||
|
||||
void eat(in uint i, in string name, Mutex[] forks) {
|
||||
void eat(in size_t i, in string name, Mutex[] forks) {
|
||||
writeln(name, " is hungry.");
|
||||
|
||||
immutable j = (i + 1) % forks.length;
|
||||
|
||||
// Take forks i and j. The lower one first to prevent deadlock.
|
||||
auto fork1 = forks[min(i, j)];
|
||||
auto fork2 = forks[max(i, j)];
|
||||
|
||||
fork1.lock();
|
||||
scope(exit) fork1.unlock();
|
||||
fork1.lock;
|
||||
scope(exit) fork1.unlock;
|
||||
|
||||
fork2.lock();
|
||||
scope(exit) fork2.unlock();
|
||||
fork2.lock;
|
||||
scope(exit) fork2.unlock;
|
||||
|
||||
writeln(name, " is eating.");
|
||||
writeln(name, " is full.");
|
||||
|
|
@ -25,16 +24,16 @@ void think(in string name) {
|
|||
}
|
||||
|
||||
void main() {
|
||||
const philosophers = "Aristotle Kant Spinoza Marx Russell".split();
|
||||
const philosophers = "Aristotle Kant Spinoza Marx Russell".split;
|
||||
Mutex[philosophers.length] forks;
|
||||
foreach (ref fork; forks)
|
||||
fork = new Mutex();
|
||||
fork = new Mutex;
|
||||
|
||||
defaultPoolThreads = forks.length;
|
||||
foreach (i, philo; taskPool.parallel(philosophers)) {
|
||||
foreach (_; 0 .. 100) {
|
||||
foreach (immutable _; 0 .. 100) {
|
||||
eat(i, philo, forks);
|
||||
think(philo);
|
||||
philo.think;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue