Posted to tcl by Whiskey at Thu Mar 21 13:53:14 GMT 2013view raw

  1. # Set packages
  2. package require Thread
  3.  
  4. # Create ThreadStarter
  5. proc Start {one two three} {
  6.  
  7. # Set MasterID
  8. set mid [thread::id]
  9.  
  10. # Set shared variables
  11. tsv::set thread mid $mid
  12.  
  13. # Create thread
  14. set tid [thread::create {
  15.  
  16. # Set packages
  17. package require struct::queue
  18.  
  19. # Create thread proc
  20. proc Start-Thread {one two three} {
  21.  
  22. # Set variables
  23. set mid "[tsv::set thread mid]"
  24. set tid "[thread::id]"
  25.  
  26. # Send massage
  27. thread::send $mid [list puts "I was here: $tid"]
  28. }
  29.  
  30. # Create queue proc
  31. proc isQueue {one two three} {
  32.  
  33. # Create queue
  34. set queue [struct::queue]
  35.  
  36. # Put jobs in queue
  37. $queue put [list Start-Thread "$one" "$two" "$three"]
  38.  
  39. # Check for jobs in queue
  40. while {[$queue size]} {
  41. eval [$queue get]
  42. }
  43.  
  44. # Remove queue
  45. $queue destroy
  46. }
  47. thread::wait
  48. }]
  49. # Start thread
  50. thread::send -async $tid [list isQueue "$one" "$two" "$three"]
  51. }
  52.  
  53. # Test result in loop
  54. proc loop {n} {
  55. for {set i 0} {$i < $n} {incr i} {
  56. Start "1" "2" "3"
  57. }
  58. }
  59.  
  60. loop 20
  61.  
  62. after 3000
  63.  
  64. update