Files
OpenVulkano/openVulkanoCpp/Base/PauseCV.hpp
2024-12-12 18:16:44 +01:00

48 lines
889 B
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
namespace OpenVulkano
{
class PauseCV final
{
std::mutex m_mutex;
std::condition_variable m_condVar;
std::atomic_bool m_paused = false;
std::atomic_bool m_isSleeping = false;
public:
void Pause() { m_paused = true; }
void Resume()
{
{
std::unique_lock lock(m_mutex);
m_paused = false;
}
m_condVar.notify_all();
}
void Check()
{
while (m_paused)
{
m_isSleeping = true;
std::unique_lock l(m_mutex);
m_condVar.wait(l, [this]{ return !m_paused; });
}
m_isSleeping = false;
}
bool IsSleeping() const { return m_isSleeping; }
};
}