-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.cpp
More file actions
51 lines (41 loc) · 958 Bytes
/
Copy pathbuffer.cpp
File metadata and controls
51 lines (41 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// Copyright (c) 2024 Vlad Troyanker
//
#include <cassert>
#include <iostream>
#include <utility>
#include "buffer.h"
Buffer make_buffer(unsigned size)
{
assert(size > 0);
auto trackingDeleter = [](std::vector<char>* p) { // debug use only
std::cout << "Call delete for object size =" << p->size() << '\n';
delete p;
};
return std::shared_ptr<std::vector<char>>(new std::vector<char>(size));
}
void BufferQueue::enqueue(Buffer buf)
{
std::lock_guard lg(mutex_);
queue_.push_back(buf);
cond_.notify_one();
}
Buffer BufferQueue::dequeue()
{
std::unique_lock lock(mutex_);
while (queue_.empty())
cond_.wait(lock);
auto v = queue_.front();
queue_.pop_front();
return v;
}
BufferQueue::size_type BufferQueue::size() const
{
std::lock_guard lg(mutex_);
return queue_.size();
}
bool BufferQueue::empty() const
{
std::lock_guard lg(mutex_);
return queue_.empty();
}