Last active
June 1, 2016 19:43
-
-
Save yamadapc/50921b43b012d71ac190b4573e894778 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio; | |
import std.concurrency; | |
import core.thread; | |
void spawnedFunc(Tid ownerTid) | |
{ | |
// Receive a message from the owner thread. | |
while (!receiveTimeout(1.dur!"seconds", | |
(int i) { | |
writeln("Received the number ", i); | |
} | |
)) { | |
writefln("spawnedFunc - LOGICAL-THREAD: %s OS-THREAD: %s", thisTid, Thread.getThis().id); | |
stdout.flush(); | |
} | |
// Send a message back to the owner thread | |
// indicating success. | |
send(ownerTid, true); | |
} | |
void main() | |
{ | |
auto runMain() { | |
// Start spawnedFunc in a new thread. | |
auto childTid = spawn(&spawnedFunc, thisTid); | |
// Send the number 42 to this new thread. | |
for(int i = 0; i < 10; i++) { | |
writefln("main - LOGICAL-THREAD: %s OS-THREAD: %s", thisTid, Thread.getThis().id); | |
stdout.flush(); | |
yield(); | |
Thread.sleep(1.dur!"seconds"); | |
} | |
send(childTid, 42); | |
// Receive the result code. | |
auto wasSuccessful = receiveOnly!(bool); | |
assert(wasSuccessful); | |
writeln("Successfully printed number."); | |
}; | |
version (fibers) { | |
writeln("Using FiberScheduler"); | |
scheduler = new FiberScheduler(); | |
scheduler.start(&runMain); | |
} else { | |
writeln("Using ThreadScheduler"); | |
runMain(); | |
} | |
} |
Author
yamadapc
commented
Jun 1, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment