Calling a member function in a new thread

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

Calling a member function in a new thread

Postby voodooattack » Mar 13, 2008 1:33

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:

Postby KristopherWindsor » Mar 13, 2008 2:07

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

Postby Pritchard » Mar 13, 2008 3:11

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

Postby D.J.Peters » Mar 13, 2008 13:01

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: 351
Joined: Oct 23, 2007 12:06
Location: Austria
Contact:

Postby Cherry » Mar 13, 2008 16:08

You are using "..threadwait". What does the prefix ".." mean?
Pritchard
Posts: 5492
Joined: Sep 12, 2005 20:06
Location: Ohio, USA

Postby Pritchard » Mar 13, 2008 16:25

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:

Postby voodooattack » Mar 15, 2008 8:56

Cherry, as Pritchard 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 Pritchard.

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

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 0 guests