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

Windows specific forum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
STARGÅTE
Addict
Addict
Posts: 2234
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post by STARGÅTE »

There is no Delay in your Test() procedure.

Add Delay(1) and give the cpu a short break :wink:
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post by IdeasVacuum »

Now that's interesting STARGÅTE ~ it certainly works with the sample code. Thank you for the tip.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

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

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

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

Post by IdeasVacuum »

Thanks Kukulkan.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply