Is there a way to call scope guard without throw exception?

David Nadlinger code at klickverbot.at
Mon Jan 1 03:06:42 UTC 2018


On Saturday, 30 December 2017 at 13:48:16 UTC, ChangLong wrote:
> After fiber yield, the spoke guard is not able to execute,  
> unless I throw a exception in Fiber.  I am look if there is 
> some hack method to make the fiber Interrupted at any time with
>  scope(exit) code executed.

There isn't. In fact, ensuring that the scope isn't left is the 
whole point of fibre context switches – how else would execution 
continue after the fibre is returned to? (How would objects be 
"un-destroyed"?)

However, there is nothing stopping you from creating a registry 
of resources yourself, and then wrapping the Fiber.yield() call 
into something like:

void myYield() {
   registry.releaseResources();
   Fiber.yield();
   registry.reacquireResources();
}

You could have the resource handles be structs that register 
themselves with the registry to integrate with fibre scheduling, 
and that also have a destructor to execute the cleanup when the 
scope is left (like `scope(exit)`).

  — David


More information about the Digitalmars-d mailing list