Skip to content

Instantly share code, notes, and snippets.

@mrmonday
mrmonday / test.d
Last active September 12, 2015 15:07
void main(string[] args)
{
struct Foo(float f) {
alias VAL = f;
float getF() {
return f;
}
}
Foo!(float.nan) f;
void main(string[] args)
{
// Check for valid arguments
enforce(args.length == 3);
int numPeople = to!int(args[1]);
int numNutrients = to!int(args[2]);
StopWatch sw;
// Time feeding with threads
sw.start();
void feedWithFibers(int numPeople, int numNutrients)
{
size_t terminated;
auto fibers = new FeedFiber[numPeople];
foreach (ref f; fibers)
{
f = new FeedFiber(new Person(numNutrients));
}
while (terminated != fibers.length)
class FeedFiber : Fiber
{
Person mPerson;
this(Person p)
{
mPerson = p;
super(&run);
}
void run()
class Person
{
struct Nutrients
{
int Fiber;
int Calcium;
int Iron;
}
int mNumNutrients;
Nutrients mNutrients;
variable amountOfBreathToTake;
function breathe()
{
do
breathe amountOfBreathToTake;
amountOfBreathToTake = 0;
Fiber.yield();
while alive;
}
function walk()
variable amountOfBreathToTake;
function breathe()
{
do
breathe amountOfBreathToTake;
amountOfBreathToTake = 0;
while alive;
}
function walk()
{
// Create a thread for both tasks so they can execute at once
breathingThread = new Thread(breathe);
walkingThread = new Thread(walk);
// Start doing both of the tasks at the same time
breathingThread.start();
walkingThread.start();
function breathe()
{
// We have to breathe to survive
}
function walk()
{
// I need to get somewhere
}
// Copyright Robert Clipsham 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module main;
import core.thread;
import std.conv;
import std.datetime;