IVGCVSW-6258 'Unittest failures'

* Fixed unit test failures happening on threads.

Signed-off-by: Sadik Armagan <sadik.armagan@arm.com>
Change-Id: I2a6048f75ece4a9f4c2116306838ff55385aabe7
diff --git a/src/armnn/test/ProfilerTests.cpp b/src/armnn/test/ProfilerTests.cpp
index 8fd9717..b23ac1c 100644
--- a/src/armnn/test/ProfilerTests.cpp
+++ b/src/armnn/test/ProfilerTests.cpp
@@ -7,6 +7,7 @@
 
 #include <doctest/doctest.h>
 
+#include <algorithm>
 #include <thread>
 
 #include <Profiling.hpp>
@@ -90,15 +91,17 @@
 TEST_CASE("RegisterUnregisterProfilerMultipleThreads")
 {
     bool res[3] = {false, false, false};
-    std::thread thread1([&res]() { RegisterUnregisterProfilerSingleThreadImpl(res[0]); });
-    std::thread thread2([&res]() { RegisterUnregisterProfilerSingleThreadImpl(res[1]); });
-    std::thread thread3([&res]() { RegisterUnregisterProfilerSingleThreadImpl(res[2]); });
+    std::vector<std::thread> threads;
+    for (unsigned int i = 0; i < 3; ++i)
+    {
+        threads.push_back(std::thread([&res, i]() { RegisterUnregisterProfilerSingleThreadImpl(res[i]); }));
+    }
+    std::for_each(threads.begin(), threads.end(), [](std::thread& theThread)
+    {
+        theThread.join();
+    });
 
-    thread1.join();
-    thread2.join();
-    thread3.join();
-
-    for (int i = 0 ; i < 3 ; i++)
+    for (int i = 0 ; i < 3 ; ++i)
     {
         CHECK(res[i]);
     }
diff --git a/src/profiling/test/ProfilingGuidTest.cpp b/src/profiling/test/ProfilingGuidTest.cpp
index caec210..30cfae8 100644
--- a/src/profiling/test/ProfilingGuidTest.cpp
+++ b/src/profiling/test/ProfilingGuidTest.cpp
@@ -7,6 +7,8 @@
 
 #include <common/include/LabelsAndEventClasses.hpp>
 
+#include <algorithm>
+#include <functional>
 #include <set>
 #include <doctest/doctest.h>
 #include <fmt/format.h>
@@ -133,25 +135,26 @@
     }
 }
 
+void GenerateProfilingGUID(ProfilingGuidGenerator& guidGenerator)
+{
+    for (int i = 0; i < 1000; ++i)
+    {
+        guidGenerator.NextGuid();
+    }
+}
+
 TEST_CASE("ProfilingGuidThreadTest")
 {
     ProfilingGuidGenerator profilingGuidGenerator;
-
-    auto guidGenerator = [&profilingGuidGenerator]()
+    std::vector<std::thread> threads;
+    for (unsigned int i = 0; i < 3; ++i)
     {
-        for (int i = 0; i < 1000; ++i)
-        {
-            profilingGuidGenerator.NextGuid();
-        }
-    };
-
-    std::thread t1(guidGenerator);
-    std::thread t2(guidGenerator);
-    std::thread t3(guidGenerator);
-
-    t1.join();
-    t2.join();
-    t3.join();
+        threads.push_back(std::thread(GenerateProfilingGUID, std::ref(profilingGuidGenerator)));
+    }
+    std::for_each(threads.begin(), threads.end(), [](std::thread& theThread)
+    {
+        theThread.join();
+    });
 
     uint64_t guid = profilingGuidGenerator.NextGuid();
     CHECK(guid == 3000u);
diff --git a/src/profiling/test/ProfilingTests.cpp b/src/profiling/test/ProfilingTests.cpp
index e0629b3..5e9a5e7 100644
--- a/src/profiling/test/ProfilingTests.cpp
+++ b/src/profiling/test/ProfilingTests.cpp
@@ -42,6 +42,7 @@
 
 #include <doctest/doctest.h>
 
+#include <algorithm>
 #include <cstdint>
 #include <cstring>
 #include <iostream>
@@ -534,17 +535,15 @@
 
     ProfilingStateMachine profilingState17(ProfilingState::Uninitialised);
 
-    std::thread thread1(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
-    std::thread thread2(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
-    std::thread thread3(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
-    std::thread thread4(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
-    std::thread thread5(ProfilingCurrentStateThreadImpl, std::ref(profilingState17));
-
-    thread1.join();
-    thread2.join();
-    thread3.join();
-    thread4.join();
-    thread5.join();
+    std::vector<std::thread> threads;
+    for (unsigned int i = 0; i < 5; ++i)
+    {
+        threads.push_back(std::thread(ProfilingCurrentStateThreadImpl, std::ref(profilingState17)));
+    }
+    std::for_each(threads.begin(), threads.end(), [](std::thread& theThread)
+    {
+        theThread.join();
+    });
 
     CHECK((profilingState17.GetCurrentState() == ProfilingState::NotConnected));
 }
@@ -703,36 +702,36 @@
     std::vector<std::thread> writers;
 
     CHECK(!counters.empty());
+    uint16_t inferencesRun = armnn::profiling::INFERENCES_RUN;
 
     // Test GetAbsoluteCounterValue
     for (int i = 0; i < 4; ++i)
     {
         // Increment and decrement the INFERENCES_RUN counter 250 times
-        writers.push_back(std::thread([&profilingService]()
+        writers.push_back(std::thread([&profilingService, inferencesRun]()
                                       {
                                           for (int i = 0; i < 250; ++i)
                                           {
-                                              profilingService.IncrementCounterValue(INFERENCES_RUN);
+                                              profilingService.IncrementCounterValue(inferencesRun);
                                           }
                                       }));
         // Add 10 to the INFERENCES_RUN counter 200 times
-        writers.push_back(std::thread([&profilingService]()
+        writers.push_back(std::thread([&profilingService, inferencesRun]()
                                       {
                                           for (int i = 0; i < 200; ++i)
                                           {
-                                              profilingService.AddCounterValue(INFERENCES_RUN, 10);
+                                              profilingService.AddCounterValue(inferencesRun, 10);
                                           }
                                       }));
         // Subtract 5 from the INFERENCES_RUN counter 200 times
-        writers.push_back(std::thread([&profilingService]()
+        writers.push_back(std::thread([&profilingService, inferencesRun]()
                                       {
                                           for (int i = 0; i < 200; ++i)
                                           {
-                                              profilingService.SubtractCounterValue(INFERENCES_RUN, 5);
+                                              profilingService.SubtractCounterValue(inferencesRun, 5);
                                           }
                                       }));
     }
-
     std::for_each(writers.begin(), writers.end(), mem_fn(&std::thread::join));
 
     uint32_t absoluteCounterValue = 0;
@@ -749,38 +748,38 @@
     writers.clear();
     uint32_t deltaCounterValue = 0;
     //Start a reading thread to randomly read the INFERENCES_RUN counter value
-    std::thread reader([&profilingService](uint32_t& deltaCounterValue)
+    std::thread reader([&profilingService, inferencesRun](uint32_t& deltaCounterValue)
                        {
                            for (int i = 0; i < 300; ++i)
                            {
-                               deltaCounterValue += profilingService.GetDeltaCounterValue(INFERENCES_RUN);
+                               deltaCounterValue += profilingService.GetDeltaCounterValue(inferencesRun);
                            }
                        }, std::ref(deltaCounterValue));
 
     for (int i = 0; i < 4; ++i)
     {
         // Increment and decrement the INFERENCES_RUN counter 250 times
-        writers.push_back(std::thread([&profilingService]()
+        writers.push_back(std::thread([&profilingService, inferencesRun]()
                                       {
                                           for (int i = 0; i < 250; ++i)
                                           {
-                                              profilingService.IncrementCounterValue(INFERENCES_RUN);
+                                              profilingService.IncrementCounterValue(inferencesRun);
                                           }
                                       }));
         // Add 10 to the INFERENCES_RUN counter 200 times
-        writers.push_back(std::thread([&profilingService]()
+        writers.push_back(std::thread([&profilingService, inferencesRun]()
                                       {
                                           for (int i = 0; i < 200; ++i)
                                           {
-                                              profilingService.AddCounterValue(INFERENCES_RUN, 10);
+                                              profilingService.AddCounterValue(inferencesRun, 10);
                                           }
                                       }));
         // Subtract 5 from the INFERENCES_RUN counter 200 times
-        writers.push_back(std::thread([&profilingService]()
+        writers.push_back(std::thread([&profilingService, inferencesRun]()
                                       {
                                           for (int i = 0; i < 200; ++i)
                                           {
-                                              profilingService.SubtractCounterValue(INFERENCES_RUN, 5);
+                                              profilingService.SubtractCounterValue(inferencesRun, 5);
                                           }
                                       }));
     }