1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
7 #include <gtest/gtest.h>
8 #include <gmock/gmock-spec-builders.h>
11 #include <condition_variable>
13 using namespace InferenceEngine;
14 using namespace ::testing;
18 bool _isThreadStarted;
19 std::mutex _isThreadStartedMutex;
20 std::condition_variable _isThreadStartedCV;
22 bool _isThreadFinished;
23 std::mutex _isThreadFinishedMutex;
24 std::condition_variable _isThreadFinishedCV;
27 std::function<void()> _function;
29 bool exceptionWasThrown;
31 MetaThread(std::function<void()> function)
32 : _function(function), _isThreadStarted(false), exceptionWasThrown(false), _isThreadFinished(false) {
33 _thread = std::thread([this]() {
34 _isThreadStarted = true;
35 _isThreadStartedCV.notify_all();
39 exceptionWasThrown = true;
41 _isThreadFinished = true;
42 _isThreadFinishedCV.notify_all();
50 void waitUntilThreadStarted() {
51 std::unique_lock<std::mutex> lock(_isThreadStartedMutex);
52 _isThreadStartedCV.wait(lock, [this]() { return _isThreadStarted; });
55 void waitUntilThreadFinished() {
56 std::unique_lock<std::mutex> lock(_isThreadFinishedMutex);
57 _isThreadFinishedCV.wait(lock, [this]() { return _isThreadFinished; });
61 if (_thread.joinable()) _thread.join();
64 typedef std::shared_ptr<MetaThread> Ptr;