Qbasicnews.com
Internal Threading? - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Internal Threading? (/thread-7053.html)

Pages: 1 2 3


Internal Threading? - Mitthrawnuruodo - 04-24-2005

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?


Internal Threading? - shiftLynx - 04-24-2005

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


Internal Threading? - Mitthrawnuruodo - 04-24-2005

No I don't understand.


Internal Threading? - v3cz0r - 04-24-2005

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.


Internal Threading? - Mitthrawnuruodo - 04-25-2005

Ok, thnx. I'll look into it.


Internal Threading? - Mitthrawnuruodo - 04-26-2005

Ok, got a question, how do I pass parameters through to the functions in the threads?


Internal Threading? - Mitthrawnuruodo - 04-26-2005

Will, no one help me?

please.

I'm trying to pass an array through it....how do you have parameters? :-?


Internal Threading? - lillo - 04-26-2005

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.


Internal Threading? - etko - 04-26-2005

Is multithreading availible in dos port?


Internal Threading? - Antoni Gual - 04-26-2005

Multithreading is a service of the OS. DOS does'nt have this kind of services.