Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Created December 1, 2022 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PetarKirov/18d73f4c6ebd283ee3675513458084ff to your computer and use it in GitHub Desktop.
Save PetarKirov/18d73f4c6ebd283ee3675513458084ff to your computer and use it in GitHub Desktop.
Switching fiber execution to a different thread
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