Threading questions

General FreeBASIC programming questions.
Post Reply
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Threading questions

Post by Emulog »

Ive tried to use threadcreate, like this "TASK=THREADCREATE(@SCRSHOW2,CPtr(Any Ptr, 0))"
My first attempt was to just launch and run it in parallel, until program end. Results are obvious, CPU have two threads at 100%. Everything fine.
A very small and fast sub scrshow2 perform at around 5000 FPS.
To reduce CPU load i moved it inside main loop and added "ThreadWait(TASK)" after threadcreate
Help file state that : "During the wait, no CPU time is consumed by the caller."
Alas it is not true.
My test show that my cpu emulation code run at effective 172 Mhz without scrshow2, 115 MHz with inline variant, and 152Mhz with passive 100% thread running. With ThreadWait subsequent to threadcreate inside loop speed falls to 62 MHz.
Next i think that i need just to issue thread creation inside loop and let the threaded code just end.
It surely ends BEFORE next occurence of threadcall in a loop. But actually this case runs less than half a second and crash.
With visible data corruption of the image being drawn to screen.
Then a question arise, are there any ways to make threaded code syncronize to screen refresh or another frq,while no consuming 100% of time ?
The "sleep" command is somewhat inconsistant, also i have a Google Chrome running, which change tick speed.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions

Post by fxm »

Emulog wrote: Feb 18, 2023 7:24 Next i think that i need just to issue thread creation inside loop and let the threaded code just end.
It surely ends BEFORE next occurence of threadcall in a loop. But actually this case runs less than half a second and crash.

When you do not use ThreadWait, try to create the thread with these two lines:
#include "fbthread.bi"
.....
TASK = THREADCREATE(@SCRSHOW2, CPtr(Any Ptr, 0))
THREADDETACH(TASK)
.....
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Threading questions

Post by Emulog »

fxm wrote: Feb 18, 2023 8:50 try to create the thread with these two lines:
Same result in crash. Plain threadcreate crashes at some point, with threaddetach just at somewhat dozen ms later.

Code: Select all

SEC=Timer
LOADSTATE  'asm code
Do
#Include "EMU_MICROFRAME.EXT" ' asm code

TASK = ThreadCreate(@SCRSHOW2, CPtr(Any Ptr, 0))
'  THREADWAIT(TASK)
'  THREADDETACH(TASK)

K$=InKey:If K=" " Then VCYC=1:SCROLLOC:Print " *** END *** "
VCYC-=1:Loop While VCYC:SEC=(Timer-SEC)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions

Post by fxm »

Emulog wrote: Feb 18, 2023 7:24 To reduce CPU load i moved it inside main loop and added "ThreadWait(TASK)" after threadcreate
Help file state that : "During the wait, no CPU time is consumed by the caller."
Alas it is not true.

I wrote 3 small programs to compare the execution times of a code block (stop execution by any key), and I found about the same value for each (about 38 ms with my PC):

Code: Select all

Sub test(Byval p As Any Ptr)
    Dim As Integer Ptr pcount = p
    While Inkey <> ""
    Wend
    While Inkey = ""
        Dim As Double t
        t = Timer
        For I As Integer = 1 To 10000000
            *pcount += 1
        Next I
        t = Timer - t
        Locate 1, 1
        Print t
    Wend
End Sub

Dim As Integer count

test(@count)

Code: Select all

#include "fbthread.bi"

Sub test(Byval p As Any Ptr)
    Dim As Integer Ptr pcount = p
    While Inkey <> ""
    Wend
    While Inkey = ""
        Dim As Double t
        t = Timer
        For I As Integer = 1 To 10000000
            *pcount += 1
        Next I
        t = Timer - t
        Locate 1, 1
        Print t
    Wend
End Sub

Dim As Integer count

Dim As Any Ptr pthread = Threadcreate(@test, @count)
Threaddetach(pthread)

Sleep

Code: Select all

Sub test(Byval p As Any Ptr)
    Dim As Integer Ptr pcount = p
    While Inkey <> ""
    Wend
    While Inkey = ""
        Dim As Double t
        t = Timer
        For I As Integer = 1 To 10000000
            *pcount += 1
        Next I
        t = Timer - t
        Locate 1, 1
        Print t
    Wend
End Sub

Dim As Integer count

Dim As Any Ptr pthread = Threadcreate(@test, @count)

Threadwait(pthread)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions

Post by fxm »

Emulog wrote: Feb 18, 2023 7:24 It surely ends BEFORE next occurence of threadcall in a loop. But actually this case runs less than half a second and crash.

Are you sure ?
See this post:
Interesting to see the Tmin and Tmax between the executing start of thread body and the returning point of Threadcreate()
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Threading questions

Post by Emulog »

Yes, in all three, sub body perform same, mine result ~60ms
But this way threadwait still consume time as i found, with more delay in execution :
PS Some minutes later i will measure actual times for my codes.

Code: Select all

#include "fbthread.bi"

Dim Shared As ULongInt a,b,c
Dim AS DOUBLE SEC,SEC2
Dim As Any Ptr pthread
Dim As Integer count

Sub test(Byval p As Any Ptr)
    Dim As Integer Ptr pcount = p
        Dim As Double t
        t = Timer
        For I As Integer = 1 To 100000
            *pcount += 1
        Next I
        t = Timer - t
        Locate 1, 1
        Print t,c
End Sub

sec=Timer
Do ' dummy load with sub and extra loop 

'  test(@count) ' plain sub take 0.226

pthread = Threadcreate(@test, @count) ' plain threadcreate 0.925

' Threaddetach(pthread) ' subseq same 0.925

Threadwait(pthread) ' subseq 1.449 

a=2000000:Do:b-=1:a-=1:Loop While a ' take 0.889 both 1.133

c+=1:Loop Until c=1000

sec=Timer-sec:Print " elapsed ";sec

Sleep
Emulog
Posts: 24
Joined: Jan 12, 2023 0:44

Re: Threading questions

Post by Emulog »

fxm wrote: Feb 18, 2023 10:30 Are you sure ?
Well, exec times are floating like hell. Despite raw speed tests, actual ones are :
threaded sub lowest 0.000276 , average time for 2000 cycles 0.00213
cycle body lowest 0.000337, average time for 2000 cycles 0.000744
Not only cache saturation affects, but a windows core issue its affection.
Now i get it.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions

Post by fxm »

Indeed, the additional loop time observed with a thread launch, compared to that with a simple subroutine call, is not due to 'ThreadWait' but rather to the time between 'ThreadCreate' and the actual start of the execution of the thread (this variable time can be longer or shorter depending on the context).
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Threading questions

Post by fxm »

Creating a new thread is an expensive act in terms of resources, both from a processor (CPU) point of view and from a memory point of view.
Also, in the event that a program requires the execution of many tasks, the creation and deletion of a thread for each of them would greatly penalize the performance of the application.
Therefore, it would be interesting to be able to share the creation of threads so that a thread that has finished executing a task is available for the execution of a future task.

That is why I defined thread pooling structures to pool the threads in order to avoid untimely creation or deletion of threads, and thus allow their reuse.
So when a task needs to be executed, it will be more resource efficient to check if the thread pool contains an available thread.
If so, it will be used while the task is running, and then freed when the task is completed.
If there is no thread available, a new thread can be created, and at the end of the task, the thread would be in turn available in the pool of threads.

I defined 3 thread pooling structures (from the simplest to the most elaborated):
- ThreadInitThenMultiStart.
- ThreadPooling.
- ThreadDispatching

See the 'Programmer's Guide' (in 'Critical Sections FAQ' article):
13. Can we emulate a kind of thread pooling feature with FreeBASIC?
(thanks again to deltarho[1859] for helping me a lot by testing my structures very exhaustively)
Post Reply