Skip to content

Instantly share code, notes, and snippets.

@bovine
Created November 11, 2016 17:11
Show Gist options
  • Save bovine/53ab96cbe2572ea1c758d604d33a1c4a to your computer and use it in GitHub Desktop.
Save bovine/53ab96cbe2572ea1c758d604d33a1c4a to your computer and use it in GitHub Desktop.
tpool::post -nowait does not ever create more than 1 worker thread
#!/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."
}
@bovine
Copy link
Author

bovine commented Nov 11, 2016

probably because the tpoolPtr->numWorkers is only checked for 0 here, unlike the else-block for when nowait is not used: https://github.com/tcltk/thread/blob/master/generic/threadPoolCmd.c#L389

@bovine
Copy link
Author

bovine commented Nov 11, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment