Fix flaky GNSS VTS

In the original code, vector::erase() would invalidate iterators so we
should move the thread object into the std::async() for the blocking
operation.

Bug: 344250967
Test: atest VtsHalGnssTargetTest
Change-Id: I4cc82131bb070a37cb6ed9dbe9d7cccc7ab5ee89
diff --git a/gnss/aidl/default/GnssMeasurementInterface.cpp b/gnss/aidl/default/GnssMeasurementInterface.cpp
index f324213..db1b8c6 100644
--- a/gnss/aidl/default/GnssMeasurementInterface.cpp
+++ b/gnss/aidl/default/GnssMeasurementInterface.cpp
@@ -146,15 +146,18 @@
     mIsActive = false;
     mGnss->setGnssMeasurementEnabled(false);
     mThreadBlocker.notify();
-    for (auto iter = mThreads.begin(); iter != mThreads.end(); ++iter) {
+    for (auto iter = mThreads.begin(); iter != mThreads.end();) {
         if (iter->joinable()) {
-            mFutures.push_back(std::async(std::launch::async, [this, iter] {
-                iter->join();
-                mThreads.erase(iter);
-            }));
-        } else {
-            mThreads.erase(iter);
+            // Store the thread object by value
+            std::thread threadToMove = std::move(*iter);
+
+            mFutures.push_back(std::async(std::launch::async,
+                                          [threadToMove = std::move(threadToMove)]() mutable {
+                                              ALOGD("joining thread");
+                                              threadToMove.join();
+                                          }));
         }
+        iter = mThreads.erase(iter);
     }
 }