Page 1 of 1

Thread safe procedure instance tree scoped variables

Posted: Wed Jan 26, 2005 4:06 am
by Axeman
Hi, what do you think about this idea?

In a threaded programming envoironment, I think it would be useful if there was a type of variable scope that allowed you to declare a variable in a procedure and have that variable uniquely available both in an instance of that procedure and also in any instances of child procedures called by that procedure instance.

Basically a variable with this scope would be global within the procedure instance tree originating from, and including, the procedure instance that it was declared in, but would not be available higher in the procedure instance tree than where it was declared. Because a variable of this scope is specific to the procedure instance rather than the procedure itself, it would also be unique in each thread created that included the procedure that declared it.

This would be a user friendly way of passing data down the procedure call tree, but its main strength would be in passing multiple data values back up the tree without using globals (and potentially causing conflicts as a result of using globals).

In the example below, I've used the term 'tree variable' to describe variables of this scope. The command 'TreeVar' is used to declare them.


---


Procedure MyChildFunc()

; Modify the values of the tree variables.
; These variables are lower in the procedure instance tree than where they were declared as tree variables
; so they are the same variables as declared in 'MyParentFunc()'
tree_variable_1 = tree_variable_1 + 2
tree_variable_2 = tree_variable_2 + 4

EndProcedure


Procedure MyParentFunc()

; Declare some tree variables.
TreeVar tree_variable_1, tree_variable_2

; Set the initial value of the tree variables.
tree_variable_1 = 1
tree_variable_2 = 2

; Call a child procedure which modifies the values of the tree variables.
MyChildFunc()

; Show the resulting values of the tree variables.
MessageRequester( "", Str( tree_variable_1 ), 0 ) ; Would output a '3'.
MessageRequester( "", Str( tree_variable_2 ), 0 ) ; Would output a '6'.

EndProcedure


Procedure MyFunc()

; Create some 'fake' tree variables.
tree_variable_1 = 10
tree_variable_2 = 20

MyParentFunc()

; The variables used here are unaffected by any changes made to the real tree variables, despite having the same names,
; as they are higher in the procedure instance tree than the point where the real tree variables were declared.
MessageRequester( "", Str( tree_variable_1 ), 0 ) ; Would output a '10'.
MessageRequester( "", Str( tree_variable_2 ), 0 ) ; Would output a '20'.

EndProcedure


MyFunc()


---


; The above code could be implemented using something equivalent to the method below (but would be a more user friendly way of doing it). There are probably better ways of implementing it, this is just an example.


Procedure MyChildFunc( *buffer )

; Modify the values of the tree variables.
PokeL( *buffer, PeekL( *buffer ) + 2 )
PokeL( *buffer + 4, PeekL( *buffer + 4 ) + 4 )

EndProcedure


Procedure MyParentFunc()

; Declare some tree variables.
*buffer = AllocateMemory( 8 )

; Set the initial value of the tree variables.
PokeL( *buffer, 1 )
PokeL( *buffer + 4, 2 )

; Call a child procedure which modifies the values of the tree variables.
MyChildFunc( *buffer )

; Show the resulting values of the tree variables
MessageRequester( "", Str( PeekL( *buffer ) ), 0 ) ; Would output a '3'.
MessageRequester( "", Str( PeekL( *buffer + 4 ) ), 0 ) ; Would output a '6'.

EndProcedure


MyParentFunc()


---