Posted to tcl by karll at Wed Mar 25 21:02:59 GMT 2015view pretty

#
# tcl thread demo
#
# creates a producer thread and uses the main thread as a consumer thread
#
# the consumer thread does a send to the producer thread to invoke blast,
# a proc that continuously generates async events to the consumer thread
#
# with the eventmark set at 5000 events the program generates 5000 thread
# sends and stops generating them and the consumer's after events fire
# as expected but no new thread events are received
#
# commenting out the eventmark set then unlimited number of the events
# are received by the consumer thread but the consumer thread's after
# events never fire
#
# the second blast proc keeps the event loop alive in the producer.
# way enabling the second blast proc with the eventmark set at 5000
# shows the same after-5000 hanging behavior of the first blast proc
#
# with no eventmark set, though, the second blast proc works as expected
#
#

package require Thread

set consumerThread [thread::id]

thread::configure $consumerThread -eventmark 5000

set producerThread [thread::create {

proc blast {thread} {
	while true {
		thread::send -async $thread hiya
	}
}

if 0 {
proc blast {thread} {
	after idle blast $thread
	thread::send -async $thread hiya
}
}

thread::wait
}]

proc periodic_timer {} {
	after 500 periodic_timer
	puts "boink"
}

proc hiya {} {
	if {[incr ::hiCount] % 1000 == 0} {
		puts $::hiCount
	}
}

periodic_timer

thread::send -async $producerThread [list blast $consumerThread]

vwait die