Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jessejcw committed Apr 21, 2017
0 parents commit 9554639
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .idea/cpp_concurrent.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 96 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.6)
project(cpp_concurrent)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(cpp_concurrent ${SOURCE_FILES})
91 changes: 91 additions & 0 deletions blocking_ring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Created by [email protected] on 2/28/17.
//

#ifndef BLOCKING_RING_H
#define BLOCKING_RING_H

#include <mutex>
#include <condition_variable>

// concurrent ring buffer
template <typename T, int SZ, typename ST=int>
class blocking_ring {
typedef T VALUE_T;
typedef ST SIZE_T;
std::mutex m_mutex;
std::condition_variable m_cond;


protected:
VALUE_T m_buffer[SZ];
SIZE_T m_size;
SIZE_T m_front;
SIZE_T m_back;
public:
blocking_ring() {
std::unique_lock<std::mutex> mlock(m_mutex);
clear();
}
~blocking_ring(){}

void push(const VALUE_T& x) {
std::unique_lock<std::mutex> mlock(m_mutex);
push();
back() = x;
mlock.unlock();
m_cond.notify_one();
}
void pop() {
std::unique_lock<std::mutex> mlock(m_mutex);
while ( m_size == 0) {
m_cond.wait(mlock);
}
m_size--;
m_front = (m_front+1) % SZ;
}
void back_erase(const SIZE_T n) {
std::unique_lock<std::mutex> mlock(m_mutex);
if (n >= m_size)
clear();
else {
m_size -= n;
m_back = (m_front + m_size -1) % SZ;
}
}
void front_erase(const SIZE_T n) {
std::unique_lock<std::mutex> mlock(m_mutex);
if (n >= m_size)
clear();
else {
m_size -= n;
m_front = (m_front+n) % SZ;
}
}

private:
SIZE_T size() const {return m_size;}
SIZE_T max_size() const { return SZ;}
bool empty() const { return m_size == 0;}
bool full() const { return m_size == SZ;}
VALUE_T& front() { return m_buffer[m_front];}
VALUE_T& back() { return m_buffer[m_back];}

void clear() {
m_size = 0;
m_front = 0;
m_back = SZ-1;
}

void push() {
m_back = (m_back+1) % SZ;
if (size() == SZ) {
m_front = (m_front + 1) % SZ;
} else {
m_size++;
}
}
};


#endif //BLOCKING_RING
42 changes: 42 additions & 0 deletions inc_me.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Created by jesse wang on 2/23/17.
//

#ifndef CODE1_INC_ME_H
#define CODE1_INC_ME_H

#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <climits>
#include <cstring>

using namespace std;

struct ListNode {
int val;
ListNode* next = nullptr;
ListNode(int v) : val(v) {}
};

struct TreeNode {
int val;
TreeNode* left = nullptr;
TreeNode* right = nullptr;
TreeNode(int v) : val(v) {}
};

struct Interval {
int start, end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};

#endif //CODE1_INC_ME_H
6 changes: 6 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>

int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}

0 comments on commit 9554639

Please sign in to comment.