title: Bash threading: wait for all job threads to finish doesn't work?
body: <p>I'm writing a little script, that will create archives in main thread and after each archive is complete, a new thread would be created by calling function that would take care of uploading these archives. The reason I want uploading to be done in background is so that another archive could be created while the previous archives are being uploaded.</p>
  
  <p>The problem I'm having is at the very end of the script. That is, main thread don't wait for all uploading threads to finish before exiting. Look at the following simplified script (I removed/changed parts of the code not related to the problem)</p>
  
  <pre><code>function func {
  for files in /home/somewhere/
    do
      echo "Uploading $1" &amp;
    done
  wait
  }
  
  find /home/some/path -type f | while read filename ; do
    echo "Creating archive of $filename"
    func $somevariable &amp;
  done
  
  wait
  </code></pre>
  
  <p>Everything is executing very nicely until the last archive is created, then the script ends before all <code>func</code> threads finish, leaving many files not uploaded.</p>
  
  <p>Thank you for your ideas.</p>