Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Internal Threading?
#11
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.
Reply
#12
Yes I did look at the example and it did NOT tell me how to pass multiple parameters.... Big Grin

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
i]"But...it was so beautifully done"[/i]
Reply
#13
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
Reply
#14
Quote: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. Big Grin
i]"But...it was so beautifully done"[/i]
Reply
#15
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:
ngelo Mottola - EC++
Reply
#16
I thought FB wasn't Multiplatform....
i]"But...it was so beautifully done"[/i]
Reply
#17
Think again. Unless you use Win32 specific libraries, any FB program can be compiled unchanged on Win32 and Linux, plus DOS32 (even though this port is younger and still not fully completed).
You should see good-old QB gorillas.bas running under Linux... 8)
ngelo Mottola - EC++
Reply
#18
Quote:I thought FB wasn't Multiplatform....


source for this engine compiled on linux without a single modification:

http://qh2.qbtk.com/525-d


Linux ver:
http://qh2.qbtk.com/527-d
y smiley is 24 bit.
[Image: anya2.jpg]

Genso's Junkyard:
http://rel.betterwebber.com/
Reply
#19
Hey, Ummm, I've been playing with the Threads, and can't get my proggie to work.

Could somebody post an example to the Threadcreate version uses types and passes a pointer to get multiple data in the thread?

thnx Big Grin
i]"But...it was so beautifully done"[/i]
Reply
#20
Sure:

Code:
type t
    a as integer
    b as integer
end type

declare sub mythread( t as t )

    dim t as t
    dim threadid as integer

    t.a = 1234
    t.b = 5678

    threadid = threadcreate( @mythread, byval @t)

    threadwait( threadid )


sub mythread( t as t )

    print "t.a ="; t.a
    print "t.b ="; t.b

end sub
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)