Multithreaded Bash

Avoiding leaks

Comments are moderated. It may take a few minutes before your comment appears.
Markdown is supported in your comments.

Let's take a closer look at jobs > /dev/null. There is a "feature" in jobs where it carries state between calls. This makes working jobs a little bit strange. Whenever a thread finishes, the thread leaves a note with jobs. The note is removed when you run jobs and see the output. However, the finished messages are not removed if you run jobs in a subshell or look at the running processes with jobs -r. So $(jobs | wc-l) accumulates cruft, and jobs > /dev/null clears it out. The cruft adds up slowly. Bash will leak 86KB for each 1000 jobs. It also makes spawning new thread O(N) instead of O(1), causing slowdowns over time.

Mail: (not shown)

Please type this: