Am 20.08.12 22:22, schrieb Googie:
> The way I see it is that the Thread package handles concurrent access
> problem entirely by having an interpreter per a thread. There is no
> concurrent access issue with any resource. Period. If you disagree -
> name one resource affected by this problem (note, that channels are
> transferable, but also exclusive to a single interpreter) and I will
> honestly respect that.
In typical low-level multithreading applications, mutexes are mostly
used to serialize access to shared memory. This is indeed not necessary
in Tcl because of the separate interpreters per thread. Also, the shared
variables (tsv:: and friends) are implicitly locked.
But note, that this holds only for resources directly managed by Tcl.
There can be external resources other than that. For instance, consider
an external file holding the result of a computation. Every time a
thread finishes computation, a column is appended to the external file.
You could do it like this:
========== 8>< ===============
set myresult [compute]
set fd [open output.dat r]
set lines [split $fd \n]
close $fd
set result {}
foreach res $myresult line $lines {
lappend result [list {*}$line $res]
}
set fd [open output.dat w]
puts -nonewline $fd [join $result \n]
close $fd
========== 8>< ================
As you can trivially see, this will result in a mess, loss of results,
strange mixing, crash... when more than one thread tries to modify the
file. This is a race condition, and it will happen, and it's *very* hard
to debug, and Tcl can nothing do to it by itself. Of course, there are
different methods to deal with it, like sending the result back to one
coordinator thread. Locking using a mutex is just another method, and
there is nothing wrong with providing that option.
Christian