Corion kindly advised my not to use Thread::Semaphore and pointed me to ... Thread::Queue.
I finally got around to putting together proof of the wisdom of Corion's advice.
This is a version of your Thread::Semaphore code, limiting to 4 concurrent threads and calculating factorials 1000! .. 2000!:
Total runtime: Took 47.511607 seconds
This version does the same calculations using the same number of concurrent threads, but ditches Thread::Semaphore in favour of Thread::Queue to queue the 1000 numbers to 4 reused threads, thus saving the startup and teardown costs of 996 threads:
Total runtime: Took 31.290966 seconds; giving a 33% saving of time.
But the biggest lesson of threading, is when not to use it. This version ditches threads altogether and uses the obvious optimisation:
Total runtime: Took 0.685503 seconds. Same results, but a 98.5% time saving over the first version above.
In reply to Re: Threads From Hell #1: How To Share A Hash [SOLVED]
by BrowserUk
in thread Threads From Hell #1: How To Share A Hash [SOLVED]
by karlgoethebier
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |