1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-01-22 08:26:48 +00:00

Not having read the C++ synchronisation primitives before, this async task queue is probably incorrect. But nevertheless, let's have a quick go at employing it — in a hideously thread unsafe fashion — for audio generation. What can possibly go wrong?

This commit is contained in:
Thomas Harte
2016-10-07 16:56:34 -04:00
parent 2d26feb073
commit e53455a936
4 changed files with 177 additions and 58 deletions

View File

@@ -0,0 +1,62 @@
//
// AsyncTaskQueue.cpp
// Clock Signal
//
// Created by Thomas Harte on 07/10/2016.
// Copyright © 2016 Thomas Harte. All rights reserved.
//
#include "AsyncTaskQueue.hpp"
using namespace Concurrency;
AsyncTaskQueue::AsyncTaskQueue() : should_destruct_(false)
{
thread_.reset(new std::thread([this]() {
while(!should_destruct_)
{
std::function<void(void)> next_function;
queue_mutex_.lock();
if(!pending_tasks_.empty())
{
next_function = pending_tasks_.front();
pending_tasks_.pop_front();
}
queue_mutex_.unlock();
if(next_function)
{
next_function();
}
else
{
std::unique_lock<std::mutex> lock(queue_mutex_);
processing_condition_.wait(lock);
lock.unlock();
}
}
}));
}
AsyncTaskQueue::~AsyncTaskQueue()
{
should_destruct_ = true;
enqueue([](){});
}
void AsyncTaskQueue::enqueue(std::function<void(void)> function)
{
queue_mutex_.lock();
pending_tasks_.push_back(function);
queue_mutex_.unlock();
std::lock_guard<std::mutex> lock(queue_mutex_);
processing_condition_.notify_all();
}
void AsyncTaskQueue::synchronise()
{
// TODO
// std::mutex
}