** Summary ** The thread [extension] brings native thread capabilities to [Tcl] ** Disambiguation ** [http://www.complang.tuwien.ac.at/forth/threaded-code.html%|%A distinct meaning] of "thread" occurs in discussions of [bytecode], especially when [Forth] is nearby. ** Attributes ** What: Thread extension Where: http://tcl.sf.net/ Where: https://core.tcl.tk/thread/timeline?y=ci Description: This Tcl extension, with a thread-enabled core, allows script level access to run Tcl scripts within threads. Currently at version 2.7.0 . Updated: 2013-01-02 Contact: See web site ** See Also ** [Threads]: [two threads run synchronously]: an example ** Tools ** [dqkit]: is a convenient deployment. [snichols]: I've had problems with [dqkit] and Tcl threads. On a Win32 system in order to find packages in a child thread I had to put the package files outside of the starkit. Because of that its almost easier to just use a thread enabled Tcl interpreter instead of dqkit if the package files have to exist outside of the [starkit]. ** Critique ** [dgp] in the [Tcl chatroom], 2013-04-05: ====== 10:41 <@ijchain> tpool::get has ability to capture the error state of a worker thread in the pool. 10:43 <@ijchain> it does look like [tpool::wait] is the only way to wait for worker thread completion. That's unfortunate. 10:45 <@ijchain> I imagine you can work around the design defect. 10:45 <@ijchain> But now I've got reasons not to use tpool. Thanks for that. ====== ** Community ** [https://lists.sourceforge.net/mailman/listinfo/tcl-threads%|%tcl-threads mailing list]: at [SourceForge]. ** Description ** ** Documentation ** [http://www.tcl.tk/doc/howto/thread_model.html%|%Tcl Threading Model]: an overview. and not the current 2.6. [http://www.beedub.com/book/4th/Threads.pdf%|%Multi-Threaded Tcl Scripts]: a sample chapter from [Book Practical Programming in Tcl and Tk%|%Practical Programming in Tcl and Tk] [http://docs.activestate.com/activetcl/8.5/thread/toc.html%|%man pages]: at [ActiveState ] [http://core.tcl.tk/thread/finfo?name=doc/html/thread.html%|%thread man page]: [http://docs.activestate.com/activetcl/8.6/thread/doc/thread.html%|%alternate (ActiveState)] ,[http://tcl.cvs.sourceforge.net/*checkout*/tcl/thread/doc/html/thread.html%|%alternate (sourceforge)] [http://core.tcl.tk/thread/finfo?name=doc/html/tpool.html%|%tpool man page]: [http://docs.activestate.com/activetcl/8.6/thread/doc/tpool.html%|%laternate (ActiveState)] ,[http://tcl.cvs.sourceforge.net/*checkout*/tcl/thread/doc/html/tpool.html%|%alternat (sourceforge)] [http://core.tcl.tk/thread/finfo?name=doc/html/tsv.html%|%tsv man page]: [http://docs.activestate.com/activetcl/8.6/thread/doc/tsv.html%|%alternate (ActiveState] ,[http://tcl.cvs.sourceforge.net/*checkout*/tcl/thread/doc/html/tsv.html%|%alternate (sourceforge)] [http://core.tcl.tk/thread/finfo?name=doc/html/ttrace.html%|%ttrace man page]: [http://docs.activestate.com/activetcl/8.6/thread/doc/ttrace.html%|%alternate (ActiveState)] ,[http://tcl.cvs.sourceforge.net/*checkout*/tcl/thread/doc/html/ttrace.html%|%alternate (sourceforge)] ** commands ** ====== package require Thread ====== : '''[thread::create]''' ?'''-joinable'''? ?'''-preserved'''? ?''script''? : '''[thread::preserve]''' ?''id''? : '''[thread::release]''' ?'''-wait'''? ?''id''? : '''[thread::id]''' : '''[thread::errorproc]''' ?''procname''? : '''[thread::unwind]''' : '''[thread::exit]''' : '''[thread::names]''' : '''[thread::exists]''' ''id'' : '''[thread::send]''' ?'''-async'''? ?'''-head'''? ''id script'' ?''varname''? : '''[thread::broadcast]''' ''id script'' : '''[thread::wait]''' : '''[thread::eval]''' ?'''-lock''' ''mutex''? ''arg'' ?''arg ...''? : '''[thread::join]''' ''id'' : '''[thread::configure]''' ''id'' ?''option''? ?''value''? ?...? : '''[thread::transfer]''' ''id channel'' : '''[thread::detach]''' ''channel'' : '''[thread::attach]''' ''channel'' : '''[thread::mutex]''' ... : '''[thread::rwmutex]''' ... : '''[thread::cond]''' ... ---- ** ttrace sub-package ** The 2.6 release adds a sub-package called Ttrace. This allows for easy propagation of interpreter related resources ([procedure]s, [namespace]s, objects (for [XOTcl])). Usage is trivial: yust wrap the code you want to replicate within ttrace::eval like this: ====== package req Thread package req Ttrace for {set i 0} {$i < 4} {incr i} { set tid($i) [thread::create -preserved] } ttrace::eval { proc foo {args} { puts foo } proc bar {args} { puts bar } } ====== and execute this from any thread created by the thread extension. This will replicate the definitions of 'foo' and 'bar' to all existing threads. In the above example, all 4 threads will be seeded. One very important design feature of Ttrace is that resources are propagated in a lazy fashion. That is, nothing is actually done in other threads until the resource gets referenced for the first time. This is accomplished by overloading the [Tcl] [unknown] command. So, when the [unknown] triggers, it tries to locate the resource definition by doing a lookup in the Ttrace private (in-memory) database first. On hit, the definition is loaded on-the-fly in the current interpreter. On miss, the [unknown] processing is delegated down-the-road. This way, thread startup and memory consumption are minimized. Thanks to [Vince Darley] for his great command [trace] framework, which is used as the base for the Trace package. For more information see 'man ttrace'. ** Threads and the event loop ** [Silas] 2006-12-17: By default threads that are not the main thread don't have an [event loop] (Am I wrong?). Do add it, insert a [[[vwait] forever]] or [[vwait ''your_variable'']] inside your '''thread::create''' block. <
> [AKM] 2006-10-26 I thought the advice was to use '''thread::wait''' so that the thread can be terminated with '''thread::release'''? ** Misc ** http://rkeene.org/projects/info/wiki/Tclkits isn't threaded. ---- [AK] 2011-09-16 11:54:21: ActiveState's basekits are threaded for OS X, Windows in all versions, and threaded for all platforms for 8.5+. ----