-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9554639
Showing
8 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |