Modules & threads

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Modules & threads

Post by jassing »

Is there a way to have a procedure called each time a new thread uses the module? (other than having each thread call it)

Sort of like:

Code: Select all

Module abc
  Procedure init()
  EndProcedure
  ThreadedCall init()
EndModule
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Modules & threads

Post by Demivec »

jassing wrote: Sun May 12, 2024 7:54 pm Is there a way to have a procedure called each time a new thread uses the module? (other than having each thread call it)

Sort of like:

Code: Select all

Module abc
  Procedure init()
  EndProcedure
  ThreadedCall init()
EndModule
Is this close to what you might want?

Code: Select all

Module abc
  Threaded abc_status = 0; 0 = uninitialized, >0 = initialized, 2 = some special status, etc.
  Procedure init()
      If abc_status = 0
          abc_status = 1
      Else
          ProcedureReturn
      EndIf
  EndProcedure
EndModule
Note: Calling the module procedures from 'non thread' code is treated collectively as its own thread.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Modules & threads

Post by jassing »

So, you can do:

Code: Select all

module blah
procedure init()
; do some stuff
endprocedure

init()
endmodule
And it will run init() the 1st time the module is included/processed.

What I'm after is a procedure call that would run 1st time for each thread.

It was a long shot, but I thought I'd ask.

I'm writing a module for someone else, and it uses both global and threaded variables, but I need the threaded variables initialized to the global ones per thread.... I'll just tell them to call ::init() in each thread. I wanted to make it idiot proof.
Something like:

Code: Select all

[module]
  global gVar
  threaded tVar

  procedure init()
    gVar =(read some value from database)
  endprocedure
  procedure initT()
    tVar =gVar
  endprocedure

  init() ; Run on module processed.
  threadCall initT(); run on thread usage
endmodule
Like I said long shot, but thought maybe someone saw some way to do it.

thanks for your time.
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Modules & threads

Post by Demivec »

jassing wrote: Sun May 12, 2024 11:18 pmLike I said long shot, but thought maybe someone saw some way to do it.

thanks for your time.
Here's my last example in a similar vein to demonstrate one way. Final implementation details would be tailored to your use of the module.

Code: Select all

Module abc
  Global gVar

  Threaded tInit = 0; 0 = uninitialized, >0 = initialized
  Threaded tVar

  Procedure init()
    gVar =(read some value from database)
  EndProcedure

  Procedure initT()
    If tInit = 0
       tVar = gVar
       tInit = 1
    Else
       ProcedureReturn
    EndIf
  EndProcedure

  ;--any other module entry procedures that rely on tVar would include a check at the beginning
  Procedure blah()
    If not tInit: initT(): Endif ;check if variable values are properly initialized
    ;remainder of procedure
  EndProcedure

  init() ; Run on module processed.
EndModule
It is only the inclusion of a single condition check. This method would be useable except in only certain time critical uses.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Modules & threads

Post by jassing »

Demivec wrote: Mon May 13, 2024 12:02 am ;--any other module entry procedures that rely on tVar would include a check at the beginning
Procedure blah()
If not tInit: initT(): Endif ;check if variable values are properly initialized
;remainder of procedure
EndProcedure
Well, once I see it seem so the obvious way to go -- I was trying to make this more difficult than needed
thank you for the reality check.
Post Reply