当前位置: 首页 > 工具软件 > Jim Tcl > 使用案例 >

Tcl在Unix/Linux环境下多线程支持环境搭建

秦学林
2023-12-01

一、 下载源码

    tcl8.5        http://tcl.tk/software/tcltk/8.5.html

    tclThread2.6 http://core.tcl.tk/thread/info/2e574064a4

二、安装tcl8.5

   

tar -xvf tcl8.5.9-src.tar.gz
cd tcl8.5.9
cd unix
./configure --prefix=$HOME/tools/tcl8.5.9 --enable-threads
make ; make install

注:要支持tcl多线程必须加编译选项 --enable-threads

 

三、 安装tclThread2.6

unzip tclthread2.6.zip
cd tclthread2.6
configure --with-tcl=$HOME/sft/tcl8.5.9/unix --prefix=$HOME/tools/tcl8.5.9/lib/tcl8.5
make ; make install

注:编译tclthread2.6时需要配置文件tclconfig,这个配置文件可以从其他安装包中获取 如mysqltcl-3.05/tclconfig。

其实目录tclconfig下有一个安装脚本名为:install-sh,内容如下:

#!/bin/sh

#
# install - install a program, script, or datafile
# This comes from X11R5; it is not part of GNU.
#
# $XConsortium: install.sh,v 1.2 89/12/18 14:47:22 jim Exp $
#
# This script is compatible with the BSD install script, but was written
# from scratch.
#


# set DOITPROG to echo to test this script

# Don't use :- since 4.3BSD and earlier shells don't like it.
doit="${DOITPROG-}"


# put in absolute paths if you don't have them in your path; or use env. vars.

mvprog="${MVPROG-mv}"
cpprog="${CPPROG-cp}"
chmodprog="${CHMODPROG-chmod}"
chownprog="${CHOWNPROG-chown}"
chgrpprog="${CHGRPPROG-chgrp}"
stripprog="${STRIPPROG-strip}"
rmprog="${RMPROG-rm}"

instcmd="$mvprog"
chmodcmd=""
chowncmd=""
chgrpcmd=""
stripcmd=""
rmcmd="$rmprog -f"
mvcmd="$mvprog"
src=""
dst=""

while [ x"$1" != x ]; do
    case $1 in
	-c) instcmd="$cpprog"
	    shift
	    continue;;

	-m) chmodcmd="$chmodprog $2"
	    shift
	    shift
	    continue;;

	-o) chowncmd="$chownprog $2"
	    shift
	    shift
	    continue;;

	-g) chgrpcmd="$chgrpprog $2"
	    shift
	    shift
	    continue;;

	-s) stripcmd="$stripprog"
	    shift
	    continue;;

	*)  if [ x"$src" = x ]
	    then
		src=$1
	    else
		dst=$1
	    fi
	    shift
	    continue;;
    esac
done

if [ x"$src" = x ]
then
	echo "install:  no input file specified"
	exit 1
fi

if [ x"$dst" = x ]
then
	echo "install:  no destination specified"
	exit 1
fi


# If destination is a directory, append the input filename; if your system
# does not like double slashes in filenames, you may need to add some logic

if [ -d $dst ]
then
	dst="$dst"/`basename $src`
fi

# Make a temp file name in the proper directory.

dstdir=`dirname $dst`
dsttmp=$dstdir/#inst.$$#

# Move or copy the file name to the temp name

$doit $instcmd $src $dsttmp

# and set any options; do chmod last to preserve setuid bits

if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; fi
if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; fi
if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; fi
if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; fi

# Now rename the file to the real destination.

$doit $rmcmd $dst
$doit $mvcmd $dsttmp $dst


exit 0


 

四、测试脚本验证tcl的多线程。

package require Thread
puts "*** I'm thread [thread::id]"
# Create 3 threads
for {set thread 1} {$thread <= 3} {incr thread} {
	set id [thread::create {
		# Print a hello message 3 times, waiting
		# a random amount of time between messages
		for {set i 1} {$i <= 3} {incr i} {
			after [expr { int(500*rand()) }]
			puts "Thread [thread::id] says hello"
		}
	}] ;# thread::create
	puts "*** Started thread $id"
} ;# for
puts "*** Existing threads: [thread::names]"
# Wait until all other threads are finished
while {[llength [thread::names]] > 1} {
	after 500
}
puts "*** That's all, folks!"


 

运行结果:

*** I'm thread tid000005EC
*** Started thread tid000016E8
*** Started thread tid00000100
*** Started thread tid00001260
*** Existing threads: tid00001260 tid00000100 tid000016E8 tid000005EC
Thread tid00000100 says hello
Thread tid000016E8 says hello
Thread tid00001260 says hello
Thread tid00000100 says hello
Thread tid00000100 says hello
Thread tid000016E8 says hello
Thread tid00001260 says hello
Thread tid00001260 says hello
Thread tid000016E8 says hello
*** That's all, folks!

 

 类似资料: