Hi,
I noticed during heavy (multi-user) stress testing the there's a problem with the TIMER function.
I looked it up on the forum, and yes, code like this is not thread-safe
OK, so I stripped the TIMER and just let the threads finish. Tested OK.
Now how can I solve this?? I looked at SetTimer but it seems that I need a handle to a window. My program is solely Dlls so I have no handle.
I thought of WAITFORMULTIPLEOBJECTS but that's where I got stuck. I must be missing something.
Here's some example code:
Yet, it doesn't wait!!! It just goes on and of course then @UDT1.result is still 0.
What am I forgetting here???
Or is there another way of doing this??
Regards
Jeroen
------------------
I noticed during heavy (multi-user) stress testing the there's a problem with the TIMER function.
I looked it up on the forum, and yes, code like this is not thread-safe
Code:
local lresult as long dim hThread(1) as local long local UDT1 as MyType PTR CoTaskMemAlloc (SIZEOF(@UDT1) @UDT1.CounterValue = 0 ' can be set to -1 by the caller to interrupt the thread and make it close @UDT1.Result = 0 ' will provide a result. If thread finishes without result, it will become -1. LOCAL Timer1 as double THREAD CREATE MyThread0 (@UDT1) TO hThread(0) SLEEP 1 THREAD CREATE MyThread1 (@UDT1) TO hThread(1) SLEEP 1 Timer1 = TIMER DO WHILE @UDT1.result = 0 SLEEP 1 IF TIMER - Timer1 > 5 THEN ' this the problem! @UDT1.CounterValue = -1 EXIT DO END IF LOOP ' handle results IF @UDT1.result = -1 THEN ' failure ELSEIF @UDT1.result > 0 then ' success of one of the threads ELSEIF @UDT1.CounterValue = -1 THEN ' END IF THREAD CLOSE hThread(0) TO lResult THREAD CLOSE hThread(1) TO lResult
Now how can I solve this?? I looked at SetTimer but it seems that I need a handle to a window. My program is solely Dlls so I have no handle.
I thought of WAITFORMULTIPLEOBJECTS but that's where I got stuck. I must be missing something.
Here's some example code:
Code:
local lresult as long dim hThread(1) as local long local UDT1 as MyType PTR CoTaskMemAlloc (SIZEOF(@UDT1) @UDT1.CounterValue = 0 ' can be set to -1 by the caller to interrupt the thread and make it close @UDT1.Result = 0 ' will provide a result. If thread finishes without result, it will become -1. THREAD CREATE MyThread0 (@UDT1) TO hThread(0) SLEEP 1 THREAD CREATE MyThread1 (@UDT1) TO hThread(1) SLEEP 1 local WaitingTime as double WaitingTime = 5000 result& = WAITFORMULTIPLEOBJECTS(2, hThread(0), %FALSE, WaitingTime) ' handle results IF @UDT1.result = -1 THEN ' failure ELSEIF @UDT1.result > 0 then ' success of one of the threads ELSEIF @UDT1.CounterValue = -1 THEN ' END IF THREAD CLOSE hThread(0) TO lResult THREAD CLOSE hThread(1) TO lResult
What am I forgetting here???
Or is there another way of doing this??
Regards
Jeroen
------------------
Comment