1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
8 #include <gtest/gtest.h>
9 #include <gmock/gmock-spec-builders.h>
12 #include <condition_variable>
14 using namespace InferenceEngine;
15 using namespace ::testing;
19 bool _isThreadStarted;
20 std::mutex _isThreadStartedMutex;
21 std::condition_variable _isThreadStartedCV;
23 bool _isThreadFinished;
24 std::mutex _isThreadFinishedMutex;
25 std::condition_variable _isThreadFinishedCV;
28 std::function<void()> _function;
30 bool exceptionWasThrown;
32 MetaThread(std::function<void()> function)
33 : _function(function), _isThreadStarted(false), exceptionWasThrown(false), _isThreadFinished(false) {
34 _thread = std::thread([this]() {
35 _isThreadStarted = true;
36 _isThreadStartedCV.notify_all();
40 exceptionWasThrown = true;
42 _isThreadFinished = true;
43 _isThreadFinishedCV.notify_all();
51 void waitUntilThreadStarted() {
52 std::unique_lock<std::mutex> lock(_isThreadStartedMutex);
53 _isThreadStartedCV.wait(lock, [this]() { return _isThreadStarted; });
56 void waitUntilThreadFinished() {
57 std::unique_lock<std::mutex> lock(_isThreadFinishedMutex);
58 _isThreadFinishedCV.wait(lock, [this]() { return _isThreadFinished; });
62 if (_thread.joinable()) _thread.join();
65 typedef std::shared_ptr<MetaThread> Ptr;