abortable sleep

This commit is contained in:
badaix 2016-11-06 18:51:29 +01:00
parent 7353383313
commit d41d314e27
5 changed files with 35 additions and 21 deletions

View file

@ -87,20 +87,29 @@ void PcmStream::start()
{
logD << "PcmStream start: " << sampleFormat_.getFormat() << "\n";
encoder_->init(this, sampleFormat_);
active_ = true;
readerThread_ = thread(&PcmStream::worker, this);
active_ = true;
thread_ = thread(&PcmStream::worker, this);
}
void PcmStream::stop()
{
if (active_)
{
active_ = false;
if (readerThread_.joinable())
readerThread_.join();
}
if (!active_ && !thread_.joinable())
return;
active_ = false;
cv_.notify_one();
if (thread_.joinable())
thread_.join();
}
bool PcmStream::sleep(int32_t ms)
{
if (ms < 0)
return true;
std::unique_lock<std::mutex> lck(mtx_);
return (cv_.wait_for(lck, std::chrono::milliseconds(ms)) == std::cv_status::timeout);
}