Posted to tcl by pooryorick at Mon Apr 18 14:56:09 GMT 2016view pretty

	if {$initialThreadReader eq $finalThreadWriter && $finalThreadWriter ne "" && [lsearch -exact $args "|"] != -1} {
		set copyThreadChannel $initialThreadReader
		set copyThread [thread::create]
		lassign [chan pipe] readerPipeReader readerPipeWriter
		lassign [chan pipe] writerPipeReader writerPipeWriter
		foreach fd [list $readerPipeReader $readerPipeWriter $writerPipeReader $writerPipeWriter] {
			fconfigure $fd -translation binary
		}
        thread::transfer $copyThreadChannel $copyThread
		thread::transfer [lindex readerpipe 1] $copyThread
		thread::transfer [lindex writerpipe 0] $copyThread
		thread::send $copyThread [string map [list @copy@ [
            list $copyThreadChannel] @reader@ [
			list $writerPipeReader] @writer@ [list $readerPipeWriter]] {

			proc copyComplete {var args} {
				incr ::copyComplete
				set sock [set ::$var]
				unset ::$var
				close $sock
			}

			proc terminate {} {
				catch {close @writer@}
				catch {close @reader@}
				thread::detach @copy@ 
			}

			fcopy @copy@ @writer@ -command [
				list copyComplete @writer@]
			fcopy @reader@ @copy -command [
				list copyComplete @reader@]
		}]
		set initialThreadReader $readerPipeReader
		set finalThreadWriter $writerPipeWriter
	}