Steve_BZ has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I've just discovered threads and ->join(): you can imagine my joy. However, I'm using it to trigger ffmpeg snapshots from a streaming video. This can become quite CPU intensive when you press the 'snap' button too many times in succession (my CPU has been running at 100% for the past five minutes). So really what I'd like to find is a discussion somewhere about design choices using threads, ->join() and others. Can I set the priority of the threads? Can I monitor the number of processes and stop more than a certain number being triggered? What else can I do? Is threads the best option? etc

Regards

Replies are listed 'Best First'.
Re: threads, ->Join() and others
by jdrago_999 (Hermit) on Sep 17, 2009 at 23:31 UTC

    I am not aware of any cross-platform way to set thread priority. However you can limit the number of threads you create by simply checking how many threads you get from threads->list and waiting to spawn a new thread until scalar(threads->list) < $max_threads.

    Something like:

    use threads; my $max_threads = 5; for( 1..20 ) { sleep(1) until scalar(threads->list) < $max_threads; threads->create( \&do_work ); } # Now wait for the remaining threads to finish: $_->join foreach threads->list;