Skip to content

Improve performance with lower sampling rate #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/consumer_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "sample.h"
#include "send_buffer.h"
#include <chrono>
#include <thread>

using namespace lsl;

Expand All @@ -23,10 +22,13 @@ consumer_queue::~consumer_queue() {
}

void consumer_queue::push_sample(const sample_p &sample) {
// acquire lock for more predictable behavior in regards to pop_sample()
std::unique_lock<std::mutex> lk(mut_);
while (!buffer_.push(sample)) {
sample_p dummy;
buffer_.pop(dummy);
}
cv_.notify_one();
}

sample_p consumer_queue::pop_sample(double timeout) {
Expand All @@ -35,12 +37,10 @@ sample_p consumer_queue::pop_sample(double timeout) {
buffer_.pop(result);
} else {
if (!buffer_.pop(result)) {
// turn timeout into the point in time at which we give up
timeout += lsl::lsl_clock();
do {
if (lsl::lsl_clock() >= timeout) break;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (!buffer_.pop(result));
// wait for a new sample until the thread calling push_sample delivers one, or until timeout
std::unique_lock<std::mutex> lk(mut_);
std::chrono::duration<double> sec(timeout);
cv_.wait_for(lk, sec, [&]{ return this->buffer_.pop(result); });
}
}
return result;
Expand Down
5 changes: 5 additions & 0 deletions src/consumer_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "common.h"
#include "forward.h"
#include <boost/lockfree/spsc_queue.hpp>
#include <mutex>
#include <condition_variable>

namespace lsl {
/**
Expand Down Expand Up @@ -52,6 +54,9 @@ class consumer_queue {
private:
send_buffer_p registry_; // optional consumer registry
buffer_type buffer_; // the sample buffer
// used to wait for new samples
std::mutex mut_;
std::condition_variable cv_;
};

} // namespace lsl
Expand Down