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

  1. #
  2. # tcl thread demo
  3. #
  4. # creates a producer thread and uses the main thread as a consumer thread
  5. #
  6. # the consumer thread does a send to the producer thread to invoke blast,
  7. # a proc that continuously generates async events to the consumer thread
  8. #
  9. # with the eventmark set at 5000 events the program generates 5000 thread
  10. # sends and stops generating them and the consumer's after events fire
  11. # as expected but no new thread events are received
  12. #
  13. # commenting out the eventmark set then unlimited number of the events
  14. # are received by the consumer thread but the consumer thread's after
  15. # events never fire
  16. #
  17. # the second blast proc keeps the event loop alive in the producer.
  18. # way enabling the second blast proc with the eventmark set at 5000
  19. # shows the same after-5000 hanging behavior of the first blast proc
  20. #
  21. # with no eventmark set, though, the second blast proc works as expected
  22. #
  23. #
  24.  
  25. package require Thread
  26.  
  27. set consumerThread [thread::id]
  28.  
  29. thread::configure $consumerThread -eventmark 5000
  30.  
  31. set producerThread [thread::create {
  32.  
  33. proc blast {thread} {
  34. while true {
  35. thread::send -async $thread hiya
  36. }
  37. }
  38.  
  39. if 0 {
  40. proc blast {thread} {
  41. after idle blast $thread
  42. thread::send -async $thread hiya
  43. }
  44. }
  45.  
  46. thread::wait
  47. }]
  48.  
  49. proc periodic_timer {} {
  50. after 500 periodic_timer
  51. puts "boink"
  52. }
  53.  
  54. proc hiya {} {
  55. if {[incr ::hiCount] % 1000 == 0} {
  56. puts $::hiCount
  57. }
  58. }
  59.  
  60. periodic_timer
  61.  
  62. thread::send -async $producerThread [list blast $consumerThread]
  63.  
  64. vwait die
  65.