Page 1 of 1

PB5.21LTS Why does this thread hammer the CPU?

Posted: Fri Mar 07, 2014 1:35 am
by IdeasVacuum
Sample code: Run as an exe, when the Thread Procedure is activated, the CPU usage jumps up by 40~50%. Anyone know why?

Code: Select all

Enumeration
#Win
#Txt
#BtnStart
#BtnStop
EndEnumeration

#PF_Thread_Time_Critical = 32
Global igStop = #False

Procedure Test(*Val)
;-------------------
Protected iCnt.i, iCurrent.i, iOld.i = ElapsedMilliseconds()


Repeat
           iCurrent = ElapsedMilliseconds()
        If iCurrent > iOld + 999

                iCnt = iCnt + 1
                SetGadgetText(#Txt, Str(iCnt))

                iOld = ElapsedMilliseconds()
        EndIf

Until(igStop = #True)

EndProcedure

Procedure Win()
;--------------

        If OpenWindow(#Win, 0, 0, 200, 100, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

                     TextGadget(#Txt,      50, 40, 100, 20, "0", #PB_Text_Center)
                   ButtonGadget(#BtnStart,  0, 80, 100, 20, "Start")
                   ButtonGadget(#BtnStop, 100, 80, 100, 20, "Stop") 
        EndIf

EndProcedure

Procedure WaitForUser()
;----------------------
Protected iEvent.i, iExit.i = #False
Protected sVal.s = Space(2) 

               Repeat
                              iEvent = WaitWindowEvent(1)
                       Select iEvent

                                     Case #PB_Event_CloseWindow: iExit = #True

                                     Case #PB_Event_Gadget

                                              Select EventGadget()

                                                     Case #BtnStart

                                                                         iThread = CreateThread(@Test(), @sVal)
                                                          ThreadPriority(iThread, #PF_Thread_Time_Critical)

                                                     Case #BtnStop

                                                          igStop = #True
                                                           iExit = #True
                                              EndSelect
                       EndSelect

               Until iExit = #True
EndProcedure

;##### MAIN
Win()
WaitForUser()

End

Re: PB5.21LTS Why does this thread hammer the CPU?

Posted: Fri Mar 07, 2014 1:45 am
by STARGÅTE
There is no Delay in your Test() procedure.

Add Delay(1) and give the cpu a short break :wink:

Re: PB5.21LTS Why does this thread hammer the CPU?

Posted: Fri Mar 07, 2014 2:06 am
by IdeasVacuum
Now that's interesting STARGÅTE ~ it certainly works with the sample code. Thank you for the tip.

Re: PB5.21LTS Why does this thread hammer the CPU?

Posted: Tue Mar 11, 2014 7:43 am
by Kukulkan
Or remove the 1 from WaitWindowEvent(1). If you give it some milliseconds value here, it returns even if no event happened. In your case, every 1ms. If you leave it empty, it will only return if some event happened. Should also help.

Kukulkan

Re: PB5.21LTS Why does this thread hammer the CPU?

Posted: Tue Mar 11, 2014 6:12 pm
by IdeasVacuum
Thanks Kukulkan.