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

ChangLong changlon at gmail.com
Tue Jan 2 07:10:14 UTC 2018


On Monday, 1 January 2018 at 03:06:42 UTC, David Nadlinger wrote:
> 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


I was looking for some thing like yieldAndThrow,  and destroy or 
reset the fiber same time, without throw exception but call the 
scope(exit).  I guess It maybe can be done with some ASM jump 
code.  (or I am wrong.)

I already ask from Learn board few days ago,  no answer yet so I 
paste it here.




More information about the Digitalmars-d mailing list