Calling a member function in a new thread

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Calling a member function in a new thread

Post by voodooattack »

Just as the title suggests, in addition to obtaining a pointer to a member procedure, we'll also call it in a new thread, just to explore the possibilities..

Code: Select all

    
    type ThreadTest
        declare property getThreadProc() as any ptr
        declare sub ThreadProc()
        declare sub ThreadStart()
        declare sub ThreadWait()
        as any ptr m_Thread
    end type
    
    property ThreadTest.getThreadProc() as any ptr
        dim r as any ptr = any
        asm
            lea eax, ThreadProc
            mov [r], eax 
        end asm
        return r
    end property
    
    
    sub ThreadTest.ThreadProc()
        print "Output from thread "; m_Thread
    end sub
    
    sub ThreadTest.ThreadStart()
        m_Thread = threadcreate(this.getThreadProc, @this)
    end sub
    
    sub ThreadTest.ThreadWait()
        ..threadwait(m_Thread)
    end sub
    
    dim v as ThreadTest ptr 
    
    v = new ThreadTest
    v->threadstart()
    v->threadwait()

    delete v
    
Note: I know this is UNSAFE, Just a hack that could prove to be useful in certain situations :)
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

This is where the "Add a Sleep command to the clipboard code" option in FInstallGUI is helpful. ;-)
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Beautiful. Thank you for this.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

point me the Trick or Tip?
(i'm blind sometimes)

Joshy

Code: Select all

    Type ThreadTest
        Declare Property   getThreadProc() As Any Ptr
        Declare static Sub Proc(Me as ThreadTest ptr)
        Declare Sub        Start()
        Declare Sub        Wait()
        private:
        As Any Ptr         m_Thread
    End Type
    
    Property ThreadTest.getThreadProc() As Any Ptr
      return @Proc
    End Property
    
    
    Sub ThreadTest.Proc(Me as ThreadTest ptr)
      for i as integer=1 to 10
        Print "Output from thread " & i
        sleep(500,1)
      next
    End Sub
    
    Sub ThreadTest.Start()
        m_Thread = ThreadCreate(this.getThreadProc, @this)
    End Sub
    
    Sub ThreadTest.Wait()
      ThreadWait(m_Thread)
    End Sub
    
    Dim v As ThreadTest Ptr 
    
    v = New ThreadTest
    v->Start()

? "wait ..."
    v->Wait()
? "ok"
    Delete v
sleep
Cherry
Posts: 358
Joined: Oct 23, 2007 12:06
Location: Austria
Contact:

Post by Cherry »

You are using "..threadwait". What does the prefix ".." mean?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

Cherry wrote:You are using "..threadwait". What does the prefix ".." mean?
It calls the threadwait in the global namespace (fb's threadwait), because he has a threadwait in his UDT. Without the .. it'd call his threadwait.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

Cherry, as anonymous1337 pointed out, the ".." (or just ".") prefix allows you to reference symbols from the global namespace, even if your local context has its own duplicate symbols with the same name.

@D.J.Peters: and your point is? this code demonstrates how to obtain pointers of non-static member procedures.. currently, the compiler does not allow this syntax-wise, and this is a way to get around it, the threading bit was just to demonstrate this, as requested by anonymous1337.

I would've probably written a more through-out example that builds vtables with GCC ABI-compatible virtual class interfaces, but unfortunately, I don't have much time at hand at the moment :)
Post Reply