Qbasicnews.com
April 12, 2021, 07:24:42 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: Internal Threading?  (Read 6324 times)
Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« on: April 24, 2005, 12:21:50 PM »

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?
Logged

i]"But...it was so beautifully done"[/i]
shiftLynx
Wandering Guru
***
Posts: 340



WWW
« Reply #1 on: April 24, 2005, 01:11:18 PM »

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
Logged

img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« Reply #2 on: April 24, 2005, 01:39:50 PM »

No I don't understand.
Logged

i]"But...it was so beautifully done"[/i]
v3cz0r
I hold this place together
*****
Posts: 924



WWW
« Reply #3 on: April 24, 2005, 03:16:19 PM »

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.
Logged

Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« Reply #4 on: April 24, 2005, 05:50:52 PM »

Ok, thnx. I'll look into it.
Logged

i]"But...it was so beautifully done"[/i]
Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« Reply #5 on: April 25, 2005, 04:28:21 PM »

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

i]"But...it was so beautifully done"[/i]
Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« Reply #6 on: April 25, 2005, 10:21:26 PM »

Will, no one help me?

please.

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

i]"But...it was so beautifully done"[/i]
lillo
Guru
**
Posts: 269


WWW
« Reply #7 on: April 26, 2005, 04:52:23 AM »

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.
Logged

ngelo Mottola - EC++
etko
Member
*
Posts: 45


WWW
« Reply #8 on: April 26, 2005, 06:26:53 AM »

Is multithreading availible in dos port?
Logged
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #9 on: April 26, 2005, 10:07:02 AM »

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

Antoni
ShadowWolf
Wandering Guru
***
Posts: 389



« Reply #10 on: April 26, 2005, 10:22:45 AM »

you could do fackish multithreading i think.

for example you could have function you want to thread
you get it's address then you have your main code with in it own function.  then you could at you entry point have some code that
would copy one of the instruction from the function that in focus at the time then copy it to a temp area then change the EIP to the location whith a return instruction and switch back and forth like that.

anyways just guessing i only have a bare basic understanding of this.
Logged
Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« Reply #11 on: April 26, 2005, 04:15:15 PM »

Yes I did look at the example and it did NOT tell me how to pass multiple parameters.... Cheesy

Anyways, how do you do that structure/pointer thing? :-? I'm sorry but I never did heard anything like that before.

Geeze I'm sounding like a n00b....heh Tongue
Logged

i]"But...it was so beautifully done"[/i]
ShadowWolf
Wandering Guru
***
Posts: 389



« Reply #12 on: April 27, 2005, 08:09:10 AM »

here a modifed version of FB example for threads using a
structor aka type.  think i did the modifcations right but i haven't even tryed compiling this but it still should give you the right idea.






''
'' 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)
''                
''
Code:


defint a-z
option explicit

'$include: 'win\kernel32.bi'
'$include: 'crt.bi'
Type CoreData
 Foo as interger
 bar as interger
 foo2 as interger
 bar2 as interger
 blas(100) as ubyte
 num as integer
end type



const THREADS   = 5
const SECS                 = 3

declare sub mythread cdecl ( byval num as integer )

        dim shared threadsRunning as integer
        Dim CoreData_ as CoreData

        dim i as integer

        '' create and call the threads
        threadsRunning = 0
        for i = 0 to THREADS-1
               Gamedata_.num = i
                if( beginthread( @mythread, 0, @coredata_ ) <> -1 ) then
                        threadsRunning = threadsRunning + 1
                end if
        next i
       
        '' wait all threads to finish
        do while( threadsRunning > 0 )
                Sleep 100
        loop
       
       
'':::::        
sub mythread cdecl ( i as CoreData ptr  )
i->foo = 234543
i->bar = 3245
i->blas(20) = 45

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

end sub
 




Logged
Mitth'raw'nuruodo
Ancient Guru
****
Posts: 515



WWW
« Reply #13 on: April 27, 2005, 04:18:08 PM »

Quote from: "v3cz0r"
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.


Ok...ummm ^. I'm getting conflicting info, which should I do, the threadcreare() or the beginthread()?

Ohhh, you mean types as structures....lol, I know what those are hehe. Cheesy
Logged

i]"But...it was so beautifully done"[/i]
lillo
Guru
**
Posts: 269


WWW
« Reply #14 on: April 27, 2005, 07:24:37 PM »

beginthread:
Pros:
- It's the Win32 API, so if you already know it, it may be more natural for you to use it.
Cons:
- You're tied to Windows, and your program will not be multiplatform.

threadcreate:
Pros:
- Integrated into FB; the built-in FB threading API also gives you easy mutexes and condition variables, the latter very hard to get by just using the Win32 API.
- Multiplatform: the same code will compile and work on both Win32 and Linux without modifications.
Cons:
I don't really see any...

You decide  :roll:
Logged

ngelo Mottola - EC++
Pages: [1] 2
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!