How to call a multithreaded UDT Sub???

General FreeBASIC programming questions.
Post Reply
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

How to call a multithreaded UDT Sub???

Post by mrminecrafttnt »

Code: Select all

type sec_integer
    dim as integer value
    declare sub mt_watchdog
    declare constructor 
end type

sub sec_integer.mt_watchdog
    do
        print "IT WORKS!"
    loop until inkey <> ""
end sub

constructor sec_integer

     threadcall sec_integer.mt_watchdog
end constructor

sleep

        
Ouput:
FBIDETEMP.bas(15) error 6: Expected '(' in 'threadcall sec_integer.mt_watchdog'
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How to call a multithreaded UDT Sub???

Post by SARG »

By adding () at the end of name.... And no need "sec_integer"

Code: Select all

threadcall mt_watchdog () 
seems to be enough.

When running libffi-6.dll is necessary otherwise error message.
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: How to call a multithreaded UDT Sub???

Post by mrminecrafttnt »

Works fine, thanks! But why crashes this simple code???

Code: Select all

type sec_integer
    dim as integer update
  
    declare sub mt_watchdog

    declare constructor
end type

    constructor sec_integer
         threadcall mt_watchdog()
    end constructor
        
    
    
    sub sec_integer.mt_watchdog
print 1
        do
            print 2
            sleep 1
            print 3
            if update = 1 then
                print 4
                do
                    print 5
                    sleep 10
                    print 6
                loop until update = 0
                print 7
            end if
            print 8

        loop until update = -1
        print 9
    end sub
    print 10
    dim as sec_integer test
    print 11
    sleep
    
    
-exx = no output???
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to call a multithreaded UDT Sub???

Post by fxm »

The SARG solution is not safe, and can only works if the thread accesses to static fields only.

If the thread must access to non static member fields, you must add a static wrapper.
In addition, as cited in the documentation, the ThreadCall working is not presently guaranteed when parameters must be passed (the object instance).
Instead use ThreadCreate.

Simple example:

Code: Select all

type sec_integer
    dim as integer value
    declare sub mt_watchdog
    declare static sub mt_watchdog_launcher (byval p as any ptr)

    declare constructor
end type

    constructor sec_integer
        threadcreate(@sec_integer.mt_watchdog_launcher, @this)
    end constructor
       
    sub sec_integer.mt_watchdog ()
        do
            print "IT WORKS!", value
            sleep 1
        loop until inkey <> ""
    end sub
    
    static sub sec_integer.mt_watchdog_launcher (byval p as any ptr)
        cptr(sec_integer ptr, p)->mt_watchdog()
    end sub

dim as sec_integer test
test.value = 1234
sleep
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: How to call a multithreaded UDT Sub???

Post by mrminecrafttnt »

This is what i need thanks :)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How to call a multithreaded UDT Sub???

Post by Tourist Trap »

mrminecrafttnt wrote:This is what i need thanks :)
You have a discussion leading to fxm's proposal here below ( of course you'll say, it's all fxm solution there ;) ):
viewtopic.php?f=2&t=23898&p=211070&hili ... 2A#p211070
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to call a multithreaded UDT Sub???

Post by fxm »

The behavior of the unsafe solution of SARG (inducing access restricted to static fields only), is very comparable to the one of the member procedure call on a null pointer.
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How to call a multithreaded UDT Sub???

Post by SARG »

My reply was just about the compile error (first post). So that's not really MY unsafe solution.... ;-)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to call a multithreaded UDT Sub???

Post by fxm »

Please excuse me.

But at the same time, I think you have found what looks like a compiler bug that should also issue an error message on that other proposed syntax:

Code: Select all

Type UDT
  Dim As Integer value
  Declare Sub thread ()
  Declare Sub thread_start ()
end type

Sub UDT.thread ()
  Do
      Print "IT WORKS!"  ', value  '' program crashes when accessing to value
  Loop Until Inkey <> ""
End Sub

Sub UDT.thread_start ()
'  Threadcall This.thread()  '' compiler error => OK
  Threadcall UDT.thread()   '' no compiler error => NOK
  Threadcall thread()       '' no compiler error => NOK
End Sub

Dim As UDT u
u.thread_start

Sleep
SARG
Posts: 1764
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How to call a multithreaded UDT Sub???

Post by SARG »

fxm wrote:Please excuse me.
No problem.
To be honnest no real running test as libffi-6.dll was missing.
fxm wrote:But at the same time, I think you have found what looks like a compiler bug that should also issue an error message on that other proposed syntax:
Find shared with mrminecrafttnt ;-)
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How to call a multithreaded UDT Sub???

Post by Tourist Trap »

fxm wrote: I think you have found what looks like a compiler bug
Unfortunately no more dkl in sight for a magic instant fix...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to call a multithreaded UDT Sub???

Post by fxm »

Compiler bug (see my above code at viewtopic.php?p=235337#p235337):
From inside a non static member procedure, ThreadCall allows to start as a thread a non static member Sub (without any parameters), which crashes only if attempting to access a non static member field (same execution behavior than calling a non static member procedure on a null pointer).
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to call a multithreaded UDT Sub???

Post by fxm »

Post Reply