#!/usr/local/bin/tclsh

package require Thread

set threadstart {
    puts "thread started"
    tsv::incr sharedData threadCount
}


# initialize the thread pool
tsv::set sharedData threadCount 0
tsv::set sharedData workersDone 0
set mypool [tpool::create -minworkers 0 -maxworkers 10 -initcmd $threadstart]

# queue up lots of varying duration work.
for {set i 0} {$i < 10} {incr i} {
    tpool::post -detached -nowait $mypool "puts \"work $i\"; after 1000; tsv::incr sharedData workersDone"
}

# wait for all of the work to finish
while {[tsv::get sharedData workersDone] < 10} {
    after 100
}

puts "releasing pool"
tpool::release $mypool

set numThreads [tsv::get sharedData threadCount]
puts "A total of $numThreads threads were created."
if {$numThreads == 1} {
    puts "BAD. All work was done serially."
} else {
    puts "GOOD. Work was done in parallel."
}
