<p>Lets say I have a loop in Bash:</p>

<pre><code>for foo in `some-command`
do
   do-something $foo
done
</code></pre>

<p><code>do-something</code> is cpu bound and I have a nice shiny 4 core processor. I'd like to be able to run up to 4 <code>do-something</code>'s at once.</p>

<p>The naive approach seems to be:</p>

<pre><code>for foo in `some-command`
do
   do-something $foo &amp;
done
</code></pre>

<p>This will run <em>all</em> <code>do-something</code>s at once, but there are a couple downsides, mainly that do-something may also have some significant I/O which performing <em>all</em> at once might slow down a bit. The other problem is that this code block returns immediately, so no way to do other work when all the <code>do-something</code>s are finished.</p>

<p>How would you write this loop so there are always X <code>do-something</code>s running at once?</p>

Instead of a plain bash, use a Makefile, then specify number of simultaneous jobs with
```
make -jX
```
where X is the number of jobs to run at once.
Or you can use
```
wait
```
("
```
man wait
```
"): launch several child processes, call
```
wait
```
- it will exit when the child processes finish.
```
```
maxjobs = 10

foreach line in `cat file.txt` {
 jobsrunning = 0
 while jobsrunning < maxjobs {
  do job &
  jobsrunning += 1
 }
wait
}

job ( ){
...
}
```
```
If you need to store the job's result, then assign their result to a variable. After
```
wait
```
you just check what the variable contains.