digitalmars.D - function is ` nogc` yet allocates closures with the GC work around!
- workman (13/13) Jan 11 2022 I need a solution to create closures on scope.
I need a solution to create closures on scope. The closures will access scope vars, but it's lifetime will be end before quit the scope. So it cloud be create on stack like value type. Is there a workaround to made this work ? ```d { int i = 0; int err = doTask(() nogc nothrow scope { i++; }); } ```
Jan 11 2022
On Tuesday, 11 January 2022 at 11:56:39 UTC, workman wrote:Is there a workaround to made this work ?Make sure `doTask` takes a `scope` delegate.
Jan 11 2022
On Tuesday, 11 January 2022 at 13:32:58 UTC, Dennis wrote:On Tuesday, 11 January 2022 at 11:56:39 UTC, workman wrote:Thanks, after add scope into doTask this work. One more question: The delegate in doTask is execute in a nogc nothrow native thread, before goto doTask I yield fiber. when native thread work done I call the fiber to continue run it. The fiber will never be touched before work done callback finished. The fiber always destroy after scope exit. In the native thread I call the delegate closures, the closures runtime stack will use native thread stack since there is no fiber switch. (bu it will read/write fiber stack vars), and all stack vars will be destroyed after delegate closures scope quit. After modify fiber stack vars from native thread, then send message to D thread, D thread call fiber again, from there it always access the updated fiber stack value?Is there a workaround to made this work ?Make sure `doTask` takes a `scope` delegate.
Jan 11 2022
On Tuesday, 11 January 2022 at 13:32:58 UTC, Dennis wrote:On Tuesday, 11 January 2022 at 11:56:39 UTC, workman wrote:To simple put, If the doTask is execute in other thread, do I need some things like volatile for fiber stack var I?Is there a workaround to made this work ?Make sure `doTask` takes a `scope` delegate.
Jan 11 2022
On Tuesday, 11 January 2022 at 13:32:58 UTC, Dennis wrote:On Tuesday, 11 January 2022 at 11:56:39 UTC, workman wrote:To simple the question further: step 1) A thread send the pointer into thread B by message step 2) Thread B modify pointer value step 3) Thread B send message to let Thread A know the work is done step 4) Thread A read the pointer and get the value. (in here Thread A always read the value updated by Thread B? )Is there a workaround to made this work ?Make sure `doTask` takes a `scope` delegate.
Jan 11 2022
On Tuesday, 11 January 2022 at 15:47:58 UTC, workman wrote:On Tuesday, 11 January 2022 at 13:32:58 UTC, Dennis wrote:Let me change your analogy and perhaps it will answer your own question. step 1) Person A shares a piece of paper with Person B step 2) Person B writes something on the piece of paper step 3) Person B gives the piece of paper back to Person A step 4) Person A reads the piece of paperOn Tuesday, 11 January 2022 at 11:56:39 UTC, workman wrote:To simple the question further: step 1) A thread send the pointer into thread B by message step 2) Thread B modify pointer value step 3) Thread B send message to let Thread A know the work is done step 4) Thread A read the pointer and get the value. (in here Thread A always read the value updated by Thread B? )Is there a workaround to made this work ?Make sure `doTask` takes a `scope` delegate.
Jan 12 2022
On Wednesday, 12 January 2022 at 14:06:53 UTC, bauss wrote:Let me change your analogy and perhaps it will answer your own question. step 1) Person A shares a piece of paper with Person B step 2) Person B writes something on the piece of paper step 3) Person B gives the piece of paper back to Person A step 4) Person A reads the piece of paperThanks for the example. I am ask here because I think this cloud be the case: 0) thread A set fiber stack var X = 0; 1) thread A send pointer X to thread B 2) thread B read pointer X, modify the X = 1 3) thread B send finish message to thread A 4) thread A read pointer X, but load X from CPU cache L1, so the value still be 0 Thread A & B will never try modify X same time, but cloud read/write value from/to CPU cache. I am not sure if this cloud be the case here.
Jan 12 2022