[Xotcl] XOTcl and Thread
Gustaf Neumann
neumann at wu.ac.at
Fri Feb 11 17:43:39 CET 2011
Am 11.02.11 15:09, schrieb Krzysztof Frukacz:
> I'll be grateful for your suggestions how to do it in XOTcl.
The following snippet creates a modem thread and a gatherer thread,
where the "gatherer" sends from time to time a SMS. The snippet shows
some common idioms when working with Tcl-threads. For just solving the
task described in the previous mail, the script can be certainly
shortened...
gustaf neumann
=======================================================================
package require XOTcl
package require Thread 2.6
package require xotcl::serializer
tsv::set tid modem [thread::create {
package require XOTcl; namespace import ::xotcl::*
#
# This is the modem implementation thread; we create here a modem
# object that might connect to the real hardware and handles dialing
# etc.
#
::xotcl::Object create modem
modem proc sendSMS {number text} {
puts "MODEM dials $number RING RING"
puts "MODEM sends text '$text'"
}
puts "MODEM waits"
thread::wait
}]
#
# Create a sample gatherer thread
#
set g1 [thread::create {
package require XOTcl; namespace import ::xotcl::*
#
# This is a thread that sends from time to time an SMS
#
::xotcl::Object create ping
ping set count 0
ping proc tick {} {
puts stderr "gatherer want to send SMS ..."
modem sendSMS +49123456789 counter=[my incr count]
after 3000 [list [self] tick]
}
puts "gatherer waits"
thread::wait
}]
#
# Create a small forwarding stub for interfacing with some
# implementation via async messages.
#
Class create ForwardStub
ForwardStub instproc unknown {method args} {
set name [namespace tail [self]]
thread::send -async [tsv::get tid $name] [list $name $method {*}$args]
}
#
# Pass the definition of class 'ForwardStub' to the gatherer. The
# serialized definition Could be used in a similar way in the init-cmd
# of a thread.
#
thread::send $g1 [::ForwardStub serialize]
#
# Create a modem forwarder in the gatherer
#
thread::send $g1 [list ::ForwardStub create modem]
#
# start the work in the gatherer thread
#
thread::send -async $g1 [list ping tick]
vwait forever
=======================================================================
--
Univ.Prof. Dr. Gustaf Neumann
Institute of Information Systems and New Media
WU Vienna
Augasse 2-6, A-1090 Vienna, AUSTRIA
More information about the Xotcl
mailing list