40-thread-mod-procs.tcl

Tcl API for Thread management provides some support for threads under NaviServer and/or AOLserver. This package contains essentially two classes THREAD and Proxy.

The class THREAD is used to create, initialize and destroy threads and to pass commands to these threads. It is designed in a way to create threads lazyly such that thread definitions can be included in the modules directory of the AOLserver and therefore be part of the AOLserver blueprints. When an instance of THREAD is created (e.g. t1), an init-command is provided. e.g.:

  ::xotcl::THREAD create t1 {
    Class create Counter -parameter {{value 1}}
    Counter instproc ++ {} {incr :value}
    Counter create c1
    Counter create c2
  }
  
Commands are sent to the thread via the "do" method, which returns the result of the command evaluated in the specified thread. When the first command is sent to a non-initialized thread, such as
  set x [t1 do c1 ++]
  
the actual thread is created and the thread ID is remembered in a tsv array. When a THREAD object is destroyed, the associated thread is terminated as well. Notice that according to the aol-server behavior it is possible to create **persistent threads** (when the thread object is created during startup and provided to all request threads through the blueprint), or to create **volatile threads** that are created during a request and which are deleted when the thread cleanup is called after some timeout. Volatile threads can shared as well (when different request-threads create the same-named thread objects) and can be used for caching proposes. Flushing the cache can be done in the thread's exitHandler. The Proxy class can be used to simplify the interaction with a thread and to hide the fact, that certain classes/objects are part of a thread. The following command creates a Proxy for an object c1 in thread t1. After this, c1 can be used like a local object.
  ::xotcl::THREAD::Proxy c1 -attach t1
  set x [c1 ++]
  
The Proxy forwards all commands to the attached thread except the methods attach, filter, detachAll and destroy. The attach method can be used to reattach a proxy instance to a different thread, such as
  c1 attach t2
  
A proxy can be (temporarily) detachted from a thread via
  c1 filter ""
  
Later forwarding to the thread can be re-enabled via
  c1 filter forward
  
When a proxy is attached to a thread and receives a destroy command, both the proxy and the corresponding object in the thread are deleted. If only the proxy object is to be destroyed, the proxy must be detachted at first. The class method detatchAll is provided to detach all proxies from their objects.
Location:
packages/xotcl-core/tcl/40-thread-mod-procs.tcl
Created:
2005-05-13
Author:
Gustaf Neumann
CVS Identification:
$Id: 40-thread-mod-procs.tcl,v 1.22 2024/09/11 06:15:56 gustafn Exp $

Procedures in this file

Detailed information

[ show source ]