TCL: two-way communication between threads in Windows

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

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

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

In the testCases.tcl file, I can use the passed in information. For instance:

 set myTestCase [lindex $argv 0] 

Inside testCases.tcl, I can put it on the handset:

 puts "$myTestCase" flush stdout 

And get what is put into the main thread using the process id:

 gets $app line 

... inside the loop.

This is not good. And not in two directions.

Does anyone know a simple way of bidirectional communication for tcl on Windows between two threads?

+4
source share
1 answer

Here is a small example that shows how two processes can interact. First from a child process (except for this as child.tcl):

 gets stdin line puts [string toupper $line] 

and then the parent process, which starts the child and comments on 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 + option to open opens the pipeline for reading and writing.

Reset is required due to pipeline buffering; this can be changed to buffer lines using the fconfigure command.

Another point; looking at your code, you are not using threads here, you are starting a child process. Tcl has an extension for streaming that provides proper cross-channel communication.

+4
source

All Articles