Page 1 of 1
Modules & threads
Posted: Sun May 12, 2024 7:54 pm
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
Re: Modules & threads
Posted: Sun May 12, 2024 9:36 pm
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.
Re: Modules & threads
Posted: Sun May 12, 2024 11:18 pm
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.
Re: Modules & threads
Posted: Mon May 13, 2024 12:02 am
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.
Re: Modules & threads
Posted: Mon May 13, 2024 8:22 am
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.