-
Notifications
You must be signed in to change notification settings - Fork 70
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
1 parent
4c9a7f1
commit d6306ef
Showing
13 changed files
with
271 additions
and
46 deletions.
There are no files selected for viewing
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,5 @@ | ||
# To shield the difference between Windows and Linux systems. | ||
|
||
*.h text eol=native | ||
*.cpp text eol=native | ||
*.inl text eol=native |
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
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
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,77 @@ | ||
/*************************** | ||
@Author: Chunel | ||
@Contact: [email protected] | ||
@File: ULockFreeRingBufferQueue.h | ||
@Time: 2023/10/7 21:35 | ||
@Desc: | ||
***************************/ | ||
|
||
#ifndef CGRAPH_ULOCKFREERINGBUFFERQUEUE_H | ||
#define CGRAPH_ULOCKFREERINGBUFFERQUEUE_H | ||
|
||
#include <atomic> | ||
#include <memory> | ||
|
||
#include "UQueueObject.h" | ||
|
||
CGRAPH_NAMESPACE_BEGIN | ||
|
||
template<typename T, CInt CAPACITY = 32> | ||
class ULockFreeRingBufferQueue : public UQueueObject { | ||
public: | ||
explicit ULockFreeRingBufferQueue() { | ||
head_ = 0; | ||
tail_ = 0; | ||
ring_buffer_.resize(CAPACITY); | ||
} | ||
|
||
~ULockFreeRingBufferQueue() override { | ||
ring_buffer_.clear(); | ||
} | ||
|
||
/** | ||
* 写入一个任务 | ||
* @param value | ||
*/ | ||
CVoid push(T&& value) { | ||
int curTail = tail_.load(std::memory_order_relaxed); | ||
int nextTail = (curTail + 1) % CAPACITY; | ||
|
||
while (nextTail == head_.load(std::memory_order_acquire)) { | ||
// 队列已满,等待其他线程出队 | ||
std::this_thread::yield(); | ||
} | ||
|
||
ring_buffer_[curTail] = std::move(value); | ||
tail_.store(nextTail, std::memory_order_release); | ||
} | ||
|
||
/** | ||
* 尝试弹出一个任务 | ||
* @param value | ||
* @return | ||
*/ | ||
CBool tryPop(T& value) { | ||
int curHead = head_.load(std::memory_order_relaxed); | ||
if (curHead == tail_.load(std::memory_order_acquire)) { | ||
// 队列已空,直接返回false | ||
return false; | ||
} | ||
|
||
value = std::move(ring_buffer_[curHead]); | ||
|
||
int nextHead = (curHead + 1) % CAPACITY; | ||
head_.store(nextHead, std::memory_order_release); | ||
return true; | ||
} | ||
|
||
|
||
private: | ||
std::atomic<CInt> head_; // 开始元素(较早写入的)的位置 | ||
std::atomic<CInt> tail_; // 尾部的位置 | ||
std::vector<std::unique_ptr<T> > ring_buffer_; // 环形队列 | ||
}; | ||
|
||
CGRAPH_NAMESPACE_END | ||
|
||
#endif //CGRAPH_ULOCKFREERINGBUFFERQUEUE_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
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
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,51 @@ | ||
/*************************** | ||
@Author: Chunel | ||
@Contact: [email protected] | ||
@File: USemaphore.h | ||
@Time: 2023/10/9 22:01 | ||
@Desc: | ||
***************************/ | ||
|
||
#ifndef CGRAPH_USEMAPHORE_H | ||
#define CGRAPH_USEMAPHORE_H | ||
|
||
CGRAPH_NAMESPACE_BEGIN | ||
|
||
#include <mutex> | ||
#include <condition_variable> | ||
|
||
#include "../UThreadObject.h" | ||
|
||
class USemaphore : public UThreadObject { | ||
public: | ||
/** | ||
* 触发一次信号 | ||
*/ | ||
CVoid signal() { | ||
CGRAPH_UNIQUE_LOCK lk(mutex_); | ||
cnt_++; | ||
if (cnt_ <= 0) { | ||
cv_.notify_one(); | ||
} | ||
} | ||
|
||
/** | ||
* 等待信号触发 | ||
*/ | ||
CVoid wait() { | ||
CGRAPH_UNIQUE_LOCK lk(mutex_); | ||
cnt_--; | ||
if (cnt_ < 0) { | ||
cv_.wait(lk); | ||
} | ||
} | ||
|
||
private: | ||
CInt cnt_ = 0; // 记录当前的次数 | ||
std::mutex mutex_; | ||
std::condition_variable cv_; | ||
}; | ||
|
||
CGRAPH_NAMESPACE_END | ||
|
||
#endif //CGRAPH_USEMAPHORE_H |
Oops, something went wrong.