thread::create

thread::create ?-joinable? ?-preserved? ?script?

Creates an interpreter in another thread. That interpreter will have the thread package pre-loaded into it, and will be the master interpreter for that thread. If supplied, script will be evaluated within the new interpreter; otherwise a default script (containing just thread::wait) will be used.

Returns the ID (as in thread::id) of the newly created thread.


[TODO: Document the -joinable and -preserved options.]

Bad Interaction With Tailcall

AMG: At least in Tcl 8.6.9 on x86_64 Linux, [thread::create] and tailcall do not seem to agree. I hoped I could create and initialize a thread like so:

set thread [thread::create [list apply {{auto_path} {
    set ::auto_path $auto_path
    package require myPackageHere
    tailcall thread::wait
}} $auto_path]]

However, when it comes time to release or cancel the thread or outright exit the program, sometimes it SIGSEGVs, locks up, aborts due to "FlushChannel: damaged channel list", or aborts due to double free or other corruption. And sometimes it even works! Totally random.

Instead, I have to create and initialize the thread using separate commands:

set thread [thread::create]
thread::send $thread [list set auto_path $auto_path]
thread::send $thread {package require myPackageHere}