olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
Condition.h
1/*
2 * Copyright (C) 2019-2021 HERE Europe B.V.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 * License-Filename: LICENSE
18 */
19
20#pragma once
21
22#include <chrono>
23#include <condition_variable>
24#include <mutex>
25
26#include <olp/core/client/CancellationContext.h>
27
28namespace olp {
29namespace client {
30
35class CORE_API Condition final {
36 public:
37 Condition() = default;
38
43 void Notify() {
44 std::unique_lock<std::mutex> lock(mutex_);
45 signaled_ = true;
46
47 // Condition should be under the lock not to run into the data
48 // race that might occur when a spurious wakeup happens in the other
49 // thread while waiting for the condition signal.
50 condition_.notify_one();
51 }
52
62 bool Wait(std::chrono::milliseconds timeout = std::chrono::seconds(60)) {
63 std::unique_lock<std::mutex> lock(mutex_);
64 bool triggered =
65 condition_.wait_for(lock, timeout, [&] { return signaled_; });
66
67 signaled_ = false;
68 return triggered;
69 }
70
71 private:
72 std::condition_variable condition_;
73 std::mutex mutex_;
74 bool signaled_{false};
75};
76
77} // namespace client
78} // namespace olp
A helper class that allows one thread to call and wait for a notification in the other thread.
Definition Condition.h:35
bool Wait(std::chrono::milliseconds timeout=std::chrono::seconds(60))
Waits for the Notify function.
Definition Condition.h:62
void Notify()
Called by the task callback to notify Wait to unblock the routine.
Definition Condition.h:43
Rules all the other namespaces.
Definition AppleSignInProperties.h:24