Created
December 1, 2022 11:36
-
-
Save PetarKirov/18d73f4c6ebd283ee3675513458084ff to your computer and use it in GitHub Desktop.
Switching fiber execution to a different thread
This file contains 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 core.thread : thread_joinAll, Thread, ThreadGroup, Fiber; | |
import std.stdio : writefln; | |
void main() | |
{ | |
writefln("Running on the main thread with id: '%s'", Thread.getThis().id); | |
auto fiber = new Fiber(() { | |
writefln("[before suspend] Running inside fiber on thread id '%s'", Thread.getThis().id); | |
Fiber.yield(); | |
writefln("[after resume] Running inside fiber on thread id '%s'", Thread.getThis().id); | |
}); | |
fiber.call(); | |
auto t = new Thread(() { | |
writefln("Running on the child thread with id: '%s'", Thread.getThis().id); | |
fiber.call(); | |
}); | |
t.start(); | |
thread_joinAll(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment