From 863a684a857581097f69811710237b8e5fa9fd36 Mon Sep 17 00:00:00 2001 From: Victor Nova Date: Tue, 3 May 2022 13:37:35 -0700 Subject: [PATCH] wait for the increments to finish in a separate thread --- src/embed_tests/Modules.cs | 41 ++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/embed_tests/Modules.cs b/src/embed_tests/Modules.cs index a88ab8552..a87d82673 100644 --- a/src/embed_tests/Modules.cs +++ b/src/embed_tests/Modules.cs @@ -51,7 +51,7 @@ public void TestEval() ps.Set("a", 1); var result = ps.Eval("a + 2"); Assert.AreEqual(3, result); - } + } } /// @@ -286,7 +286,7 @@ public void TestImportScopeFunction() public void TestVariables() { using (Py.GIL()) - { + { (ps.Variables() as dynamic)["ee"] = new PyInt(200); var a0 = ps.Get("ee"); Assert.AreEqual(200, a0); @@ -338,34 +338,49 @@ public void TestThread() " th_cnt += 1\n" ); } - int th_cnt = 100; + const int th_cnt = 100; + int locked = 0, unlocked = 0, started = 0; for (int i = 0; i < th_cnt; i++) { - System.Threading.Thread th = new System.Threading.Thread(() => + ThreadPool.QueueUserWorkItem(_ => + // var th = new System.Threading.Thread(() => { + Interlocked.Increment(ref started); using (Py.GIL()) { + Interlocked.Increment(ref locked); //ps.GetVariable("update")(); //call the scope function dynamicly _ps.update(); } + Interlocked.Increment(ref unlocked); }); - th.Start(); + // th.Start(); } //equivalent to Thread.Join, make the main thread join the GIL competition - int cnt = 0; - while (cnt != th_cnt) + int result = 0; + var wait = new Thread(() => { + int cnt = 0; + while (cnt != th_cnt) + { + using (Py.GIL()) + { + cnt = ps.Get("th_cnt"); + } + Thread.Yield(); + } + using (Py.GIL()) { - cnt = ps.Get("th_cnt"); + result = ps.Get("res"); } - Thread.Yield(); - } - using (Py.GIL()) + }); + wait.Start(); + if (!wait.Join(timeout: TimeSpan.FromSeconds(30))) { - var result = ps.Get("res"); - Assert.AreEqual(101 * th_cnt, result); + Assert.Fail($"started: {started} locked: {locked} unlocked: {unlocked}"); } + Assert.AreEqual(101 * th_cnt, result); } finally { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy