Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Internal Threading?
#1
Is there a way in FB to call a user-defined function from a main program and have that function running while your main program continues running along with it?
i]"But...it was so beautifully done"[/i]
Reply
#2
Yes, here is the example provided with FB:

[syntax="FB"]
''
'' freeBASIC threads example, using CRTDLL's beginthread routine
''

'' note: beginthread(ex) and endthread routines must be used instead of kernel32's CreateThread(ex) and ExitThread,
'' because the C run-time library has to preserve its context when any of its functions are called from
'' a thread. Using CreateThread would leak memory or cause strange behaviors.
''
'' Thread routines must be declared as CDECL (C calling convention), as that's what beginthread(ex)
'' expects (with CreateThread(ex), the convention must be STDCALL)
''
''

defint a-z
option explicit

'$include: 'win\kernel32.bi'
'$include: 'crt.bi'

const THREADS = 5
const SECS = 3

declare sub mythread cdecl ( byval num as integer )

dim shared threadsRunning as integer

dim i as integer

'' create and call the threads
threadsRunning = 0
for i = 0 to THREADS-1
if( beginthread( @mythread, 0, byval i ) <> -1 ) then
threadsRunning = threadsRunning + 1
end if
next i

'' wait all threads to finish
do while( threadsRunning > 0 )
Sleep 100
loop


'':::::
sub mythread cdecl ( byval num as integer )
dim i as integer

for i = 0 to SECS-1
print "Hello from thread: " + str$( num ) + " (" + str$( SECS-i ) + " sec(s) left)"
Sleep 1000
next i

threadsRunning = threadsRunning - 1

end sub
[/syntax]

The function you're putting into a separate thread must be of the same format as the 'mythread' subroutine in the example. Lillo will probably be able to tell you a lot more, since he implemented the support for it. Wink
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#3
No I don't understand.
i]"But...it was so beautifully done"[/i]
Reply
#4
See the examples/threads.bas, threads are supported intrinsically since 0.12, using the crt or Windows thread routines won't be safe, as the single-threaded FB runtime library will be used - when threadcreate() is used, FB will link to the multi-threading rtlib automatically.
Reply
#5
Ok, thnx. I'll look into it.
i]"But...it was so beautifully done"[/i]
Reply
#6
Ok, got a question, how do I pass parameters through to the functions in the threads?
i]"But...it was so beautifully done"[/i]
Reply
#7
Will, no one help me?

please.

I'm trying to pass an array through it....how do you have parameters? :-?
i]"But...it was so beautifully done"[/i]
Reply
#8
Have you actually looked at examples/threads.bas?? It shows how to create threads and pass parameters to them.
Basically you have one integer parameter you can pass to a thread when you create it via threadcreate; if you need to pass multiple data, a way would be to put all your data in a structure, and pass a pointer to that struct as parameter to threadcreate, so you can access the data from your thread.
ngelo Mottola - EC++
Reply
#9
Is multithreading availible in dos port?
Reply
#10
Multithreading is a service of the OS. DOS does'nt have this kind of services.
Antoni
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)