tags:

views:

712

answers:

1

I need to have two way communication between threads in Tcl and all I can get is one way with parameters passing in as my only master->helper communication channel. Here is what I have:

proc ExecProgram { command } {
    if { [catch {open "| $command" RDWR} fd ] } {
        #
        # Failed, return error indication
        #
        error "$fd"
    }
}

To call the tclsh83, for example ExecProgram "tclsh83 testCases.tcl TestCase_01"

Within the testCases.tcl file I can use that passed in information. For example:

set myTestCase [lindex $argv 0]

Within testCases.tcl I can puts out to the pipe:

puts "$myTestCase"
flush stdout

And receive that puts within the master thread by using the process ID:

gets $app line

...within a loop.

Which is not very good. And not two-way.

Anyone know of an easy 2-way communication method for tcl in Windows between 2 threads?

+2  A: 

Hi,

Here is a small example that shows how two processes can communicate. First off the child process (save this as child.tcl):

gets stdin line
puts [string toupper $line]

and then the parent process that starts the child and comunicates with it:

set fd [open "| tclsh child.tcl" r+]

puts $fd "This is a test"
flush $fd

gets $fd line
puts $line

The parent uses the value returned by open to send and receive data to/from the child process; the r+ parameter to open opens the pipeline for both read and write.

The flush is required because of the buffering on the pipeline; it is possible to change this to line buffering using the fconfigure command.

Just one other point; looking at your code you aren't using threads here you are starting a child process. Tcl has a threading extension which does allow proper interthread communications.

Jackson
Great! It works just like you said.
Fraser