|   | ![[ Previous ]](navbprev.gif)  ![[ Contents ]](navbhome.gif)  ![[ Index ]](navbhelp.gif)  ![[ Next ]](navbnext.gif)  | 
Share variable among all Tcl interpreters
ns_share -init {initscript} variable ...
Makes the specified variable(s) available to all Tcl interpreters in a virtual server. Normally, variables are only available to the current Tcl interpreter, unless the SharedGlobals parameter is On. Usually, ns_share should be called in a script designed to be run only once, such as in the init.tcl script for private or shared libraries.
Multiple variables can be specified by separating the variable names with spaces:
ns_share count length width
You can optionally specify an init script when you share a single variable (not when sharing multiple variables) to initialize the variable at the same time. The following example sets the variable count to 0 immediately after sharing it, and the next line increments it to 1:
    ns_share -init {set count 0} count
    incr count
You can initialize shared arrays like this:
    ns_share -init {
      set itemlist(a) "Value 1"
      set itemlist(b) "Value 2"
    ) itemlist
Unlike global variables, shared variables are not available by default at the global level. You must share a variable at the top level also and not just in procedures that want to reference the variable. For example:
    ns_share pagelength
    set pagelength "50"
    proc page { conn i } {
      ns_share pagelength
      ...
    }