External Thread for Blitz3D

Blitz3D Forums/Blitz3D Userlibs/External Thread for Blitz3D

ZJP(Posted 2009) [#1]
Hi,

Threads here. You can use the full power of a C2D or C2Q.
First test ;-)

Blitz3D source

Global Value%
Global Pointer ; Init pointer
Global Pointer_setting=1
Gosub Test            ; First call From Blitz3D for setting the pointer

Thread_Gosub(Pointer,1)  ; Launch the thread. Number 1 to 8

While Not KeyDown(1)
    a=a+1
    Print "This is the main " + A
    Print "Thread Value   >>" + value
    Delay 1
Wend
End 

.Test
    Pointer = Thread_Pointer()
    If Pointer_setting Then 
	Pointer_setting=0
	Return
    EndIf 
    ; Thread start here. This is a normal Gosub.  
    For Value=1 To 2000000000 ; !!!
       ; Delay 1
    Next
Return 



The DECLS

.lib "BLITZ_THREAD.DLL"

Thread_Pointer%()                     : "Thread_Pointer@0"
Thread_Gosub(Adresse%,Thread_Number%) : "Thread_Gosub@8"


And the .RAR http://www.zinfo972.net/pubip/BLITZ_THREAD.RAR

Important post http://www.blitzbasic.com/Community/posts.php?topic=83420


Ian Thompson(Posted 2009) [#2]
This seemed to quietly slip by but it is important, thread for Blitz3D!

Excellent stuff, thank you! :D

How about some functions for thread priorities, killing threads etc... please?


MikhailV(Posted 2009) [#3]
See topic - http://blitzbasic.com/Community/posts.php?topic=83420


ZJP(Posted 2010) [#4]
Hi,

Knowledge must be shared. Here the source of the Dll in FreeBasic. Have fun... ;)

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' FreeBasic Thread for B3D (only???)
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'
Sub mythread (param As UInteger Ptr)    
     Asm Call [param]
End Sub

Function FunctionPointer ALIAS "FunctionPointer" ()  AS UInteger Export
    DIM StackPosition AS UInteger
    DIM Adress AS UInteger
	Asm
      	mov [StackPosition],esp
      	mov esp,ebp
      	Add esp,4
      	pop [Adress]
      	mov esp,[StackPosition]
	End Asm

    Return Adress - 8
END FUNCTION

SUB Call_THREAD ALIAS "Call_Thread" (BYVAL Adresse AS UInteger PTR) Export
 	Dim thread As Any Ptr
	thread = ThreadCreate( @mythread,Adresse )
END Sub



.decls

.lib "BLITZFB_THREAD.DLL"

FunctionPointer%()    : "FunctionPointer@0"
Call_Thread(Adresse%) : "Call_Thread@4"


B3d test

Global Pointer ; Init pointer
Global Pointer_setting=1
Gosub Test            ; First call From Blitz3D for setting the pointer
Call_Thread(Pointer)  ; Init the thread

While Not KeyDown(1)
	a=a+1
	Print "This is the main " + A
	Delay 100
Wend
End 

.Test

	Pointer = FunctionPointer()
	If Pointer_setting Then
		Pointer_setting=0
		Return
	EndIf
	; Thread here. 
	Print "Hello, I am called from the DLL with a THREAD"
	For i=1000 To 10000
		Delay 5
		Print ">>>> CALL BY THE THREAD "+ i
    Next
Return 



Charrua(Posted 2010) [#5]
hi,
thank's

nice work!

just a curiosity, the -8 in the return sentence, is it only valid on a 32 bit system?

(I'm asuming Segment:Ofset far call 32:32 bits in total 8 bytes and for this you substract 8 forom the return address poped from the stack, isn't it?

in a 64 system, should i try -16?)

edit, nevermind, the return address is the address of the next instruction , 8 is the lenght of the Call blitz do to your function, or i'm wrong again?
btw, wouldn't be used something like

mov eax,ss:[esp+4]
mov [address],eax

with out modifing the stack (with pop) and having to save an restore it?

Juan


ZJP(Posted 2010) [#6]
"...edit, nevermind, the return address is the address of the next instruction , 8 is the lenght of the Call blitz do to your function, or i'm wrong again?..." It's right ;)

Take a look here http://www.blitzbasic.com/Community/posts.php?topic=83420

;)

JP


Charrua(Posted 2010) [#7]
thank's again, where am i when interesting threads are posted?

Juan


ZJP(Posted 2010) [#8]
Hi,

thank's again, where am i when interesting threads are posted?

Playing with a WiiMote? ;)


I forgot this version. Somewhere on my hard drive. Best. Also works in debug mode. It is the source of the first post. ;)

JP

FreeBasic Dll
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'+                                       Blitz Thread Gosub                                 +
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Sub mythread1 (param As Uinteger Ptr)    
    asm Call [param]
End Sub
'
Sub mythread2 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Sub mythread3 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Sub mythread4 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Sub mythread5 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Sub mythread6 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Sub mythread7 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Sub mythread8 (param As Uinteger ptr)    
    asm Call [param]
End Sub
'
Function Thread_Pointer alias "Thread_Pointer" ()  As Uinteger Export
    dim StackPosition As Uinteger
    dim Adress As Uinteger
    Asm
        mov [StackPosition],esp
        mov esp,ebp
        add esp,4
        pop [Adress]
        mov esp,[StackPosition]
    End Asm
    return Adress - 8
End Function
'
Sub Thread_Gosub Alias "Thread_Gosub" (Byval Adresse As Uinteger Ptr,Byval Thread_Number As Uinteger) Export
 	Dim thread1 As Any Ptr
 	Dim thread2 As Any Ptr
 	Dim thread3 As Any Ptr
 	Dim thread4 As Any Ptr
 	Dim thread5 As Any Ptr
 	Dim thread6 As Any Ptr
 	Dim thread7 As Any Ptr
 	Dim thread8 As Any Ptr
	If Thread_Number=1 Then thread1 = threadcreate( @mythread1, Adresse )
	If Thread_Number=2 Then thread2 = threadcreate( @mythread2, Adresse )
	If Thread_Number=3 Then thread3 = threadcreate( @mythread3, Adresse )
	If Thread_Number=4 Then thread4 = threadcreate( @mythread4, Adresse )
	If Thread_Number=5 Then thread5 = threadcreate( @mythread5, Adresse )
	If Thread_Number=6 Then thread6 = threadcreate( @mythread6, Adresse )
	If Thread_Number=7 Then thread7 = threadcreate( @mythread7, Adresse )
	If Thread_Number=8 Then thread8 = threadcreate( @mythread8, Adresse )
End Sub
'


.decls
.lib "BLITZ_THREAD.DLL"

Thread_Pointer%()                     : "Thread_Pointer@0"
Thread_Gosub(Adresse%,Thread_Number%) : "Thread_Gosub@8"


Blitz3d test
Global Value%
Global h%
Global e%=0
Global z#
Global Pointer ; Init pointer
Global Pointer_setting=1
Gosub Test            ; First call From Blitz3D for setting the pointer
Pointer_setting=0

Thread_Gosub(Pointer,1)  ; Init the thread. Number 1 to 8

While Not KeyDown(1)
	If KeyDown(57) Then e=1 ; stop thread with'space'
	a=a+1
	h=Rnd(0,100)
	Print "Main A " + A
	Print "Thread Value   >>" + value
	Print "Thread Z#      >>" + z
	Delay 1 ; mini delai. !!!!
Wend
End 

.Test
	; Thread here. This is a normal Gosub.  
	Pointer = Thread_Pointer()
	If Pointer_setting Then	Return 
	While (Not e) ; Stop by the main.
		z=z+0.1
		value=value+1
		If h<5 Then Print" Print here. Setting by the main !!!"
    Wend
Return 



Neochrome(Posted 2010) [#9]
looking at the code, it looks great, but its late here...
Can this be used, while loading game data, do a routine such as a rotating box for example in 3D?