From 52ddb7de1e1085f0deefdb6b8af868970d85178f Mon Sep 17 00:00:00 2001 From: Victor Milovanov Date: Thu, 27 Feb 2020 18:03:18 -0800 Subject: [PATCH] fixed synchronization in PyScopeTest.TestThread as suggested in da97502006791bb0597446766ad00a6f9d291895 fixes https://github.com/pythonnet/pythonnet/issues/1067 --- src/embed_tests/TestPyScope.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/embed_tests/TestPyScope.cs b/src/embed_tests/TestPyScope.cs index 7a4aa0228..701e698ec 100644 --- a/src/embed_tests/TestPyScope.cs +++ b/src/embed_tests/TestPyScope.cs @@ -338,16 +338,16 @@ public void TestThread() //add function to the scope //can be call many times, more efficient than ast ps.Exec( - "import clr\n" + - "from System.Threading import Thread\n" + + "import threading\n" + + "lock = threading.Lock()\n" + "def update():\n" + - " global res, th_cnt\n" + + " global res, th_cnt\n" + + " with lock:\n" + " res += bb + 1\n" + - " Thread.MemoryBarrier()\n" + " th_cnt += 1\n" ); } - int th_cnt = 3; + int th_cnt = 100; for (int i = 0; i < th_cnt; i++) { System.Threading.Thread th = new System.Threading.Thread(() => @@ -368,9 +368,8 @@ public void TestThread() { cnt = ps.Get("th_cnt"); } - Thread.Sleep(10); + Thread.Yield(); } - Thread.MemoryBarrier(); using (Py.GIL()) { var result = ps.Get("res");