Capture std{out,err} in a threadsafe way