Skip to content
Tcl Source Code View Ticket
Login

View Ticket

How to detect "unknown" command inside the thread script in proc unknown of init.tcl file

Closedd215ef73bb9930338f44b8e3fa28e188c7597c21Created 6 years agoUpdated 6 years ago
Descriptionplain2021-03-09 09:32:52
We have a "unknown" command in thread script. We have "proc unknown args" in init file. Can we detect in this proc "proc unknown args" that this "unknown" command is coming from thread script?

tcl script file:

package require  Thread
thread::create {
unknown_command
}
exit
arvind29 Ticket [d215ef73bb]: 4 changes [History diff]2021-03-10 06:00:09
sebreswiki2021-03-10 13:31:21

> Can we detect in this proc "proc unknown args" that this "unknown" command is coming from thread script?

Not really (in direct way). Tcl interpreter (as well as unknown or also other handlers) do not imply usage of any thread facilities, neither distinguish the threads (indirectly the interpreter knows only its own thread where it belongs to).
File init.tcl is loaded in every interpreter (regardless the thread) if it gets created (initialized).

But this is occured in main thread as first...
So either you would save the ID of the main thread and compare it inside your unknown handler like this:

# somewhere in main thread (or even in init.tcl):
if {![tsv::exists threads main]} {
  tsv::set threads main [thread::id]
}
...
# in unknown handler:
if {[tsv::get threads main] ne [thread::id]} {
  puts "this is helper thread"
}
Or you would set some marker for this thread (like here variable ::helper_thread):
# in unknown handler:
if {[info exists ::helper_thread] && $::helper_thread} {
  puts "this is helper thread"
}

thread::create { # mark thread as a helper thread: set ::helper_thread 1 ... unknown_command }

Just note that in this case file init.tcl is called before init-script of thread::create gets called, so setting of set ::helper_thread 1 may be too late, if unknown handler gets invoked during some initialization process (inside init.tcl invocation).

Log in (anonymous is fine) to add a comment.