-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounded_buffer.h
More file actions
42 lines (32 loc) · 792 Bytes
/
bounded_buffer.h
File metadata and controls
42 lines (32 loc) · 792 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
#ifndef BOUNDED_BUFFER_H_
#define BOUNDED_BUFFER_H_
#include <condition_variable>
#include <mutex>
#include <thread>
#include <algorithm>
#include <vector>
#include <string>
#include "sdk.h"
// https://baptiste-wicht.com/posts/2012/04/c11-concurrency-tutorial-advanced-locking-and-condition-variables.html
class BoundedBuffer {
public:
BoundedBuffer(int capacity);
~BoundedBuffer();
void Deposit(Item* item);
Item* Fetch();
private:
std::vector<Item*> buffer_;
int capacity_;
int front_;
int rear_;
int count_;
std::mutex lock_;
std::condition_variable not_full_;
std::condition_variable not_empty_;
public:
bool hasNextItem;
};
int InternalSort(Item *item);
void Produce(BoundedBuffer& buffer, int &num_file);
void Consume(BoundedBuffer& buffer, int &num_bad);
#endif