Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions src/UtilsCtrl/ThreadPool/Queue/UAtomicRingBufferQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UAtomicRingBufferQueue : public UQueueObject {
* @return
* @notice 谨慎使用,push信息之后,不推荐使用
*/
UAtomicRingBufferQueue* setCapacity(CUInt size) {
UAtomicRingBufferQueue* setCapacity(const CUInt size) {
capacity_ = size;
ring_buffer_queue_.resize(capacity_);
return this;
Expand All @@ -60,7 +60,7 @@ class UAtomicRingBufferQueue : public UQueueObject {
* @return
*/
template<class TImpl = T>
CVoid push(const std::shared_ptr<TImpl>& value, URingBufferPushStrategy strategy) {
CVoid push(const std::shared_ptr<TImpl>& value, const URingBufferPushStrategy strategy) {
{
CGRAPH_UNIQUE_LOCK lk(mutex_);
if (isFull()) {
Expand Down Expand Up @@ -90,7 +90,7 @@ class UAtomicRingBufferQueue : public UQueueObject {
* @return
*/
template<class TImpl = T>
CStatus waitPopWithTimeout(std::shared_ptr<TImpl>& value, CMSec timeout) {
CStatus waitPopWithTimeout(std::shared_ptr<TImpl>& value, const CMSec timeout) {
CGRAPH_FUNCTION_BEGIN
{
CGRAPH_UNIQUE_LOCK lk(mutex_);
Expand All @@ -101,11 +101,7 @@ class UAtomicRingBufferQueue : public UQueueObject {
CGRAPH_RETURN_ERROR_STATUS("receive message timeout.")
}

/**
* 当传入的内容,是智能指针的时候,
* 这里就直接通过 move转移过去好了,跟直接传值的方式,保持区别
*/
value = ring_buffer_queue_[head_];
value = std::move(ring_buffer_queue_[head_]);
head_ = (head_ + 1) % capacity_;
}
push_cv_.notify_one();
Expand All @@ -129,7 +125,7 @@ class UAtomicRingBufferQueue : public UQueueObject {
* 当前队列是否为满
* @return
*/
CBool isFull() {
CBool isFull() const {
// 空出来一个位置,这个时候不让 tail写入
return head_ == (tail_ + 1) % capacity_;
}
Expand All @@ -138,7 +134,7 @@ class UAtomicRingBufferQueue : public UQueueObject {
* 当前队列是否为空
* @return
*/
CBool isEmpty() {
CBool isEmpty() const {
return head_ == tail_;
}

Expand Down
10 changes: 5 additions & 5 deletions src/UtilsCtrl/ThreadPool/Queue/ULockFreeRingBufferQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ULockFreeRingBufferQueue : public UQueueObject {
*/
CVoid push(T&& value) {
int curTail = tail_.load(std::memory_order_relaxed);
int nextTail = (curTail + 1) % CAPACITY;
const int nextTail = (curTail + 1) % CAPACITY;

while (nextTail == head_.load(std::memory_order_acquire)) {
// 队列已满,等待其他线程出队
Expand All @@ -60,16 +60,16 @@ class ULockFreeRingBufferQueue : public UQueueObject {

value = std::move(ring_buffer_[curHead]);

int nextHead = (curHead + 1) % CAPACITY;
const 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_; // 环形队列
std::atomic<CInt> head_{}; // 开始元素(较早写入的)的位置
std::atomic<CInt> tail_{}; // 尾部的位置
std::vector<std::unique_ptr<T> > ring_buffer_; // 环形队列
};

CGRAPH_NAMESPACE_END
Expand Down
2 changes: 1 addition & 1 deletion src/UtilsCtrl/ThreadPool/UThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class UThreadPool : public UThreadObject {

/**
* 针对单个任务的情况,复用任务组信息,实现单个任务直接执行
* @param task
* @param func
* @param ttl
* @param onFinished
* @return
Expand Down
3 changes: 1 addition & 2 deletions src/UtilsCtrl/ThreadPool/UThreadPoolDefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#define CGRAPH_UTHREADPOOLDEFINE_H

#include <thread>
#include <mutex>
#if __cplusplus >= 201703L
#include <shared_mutex>
#endif
Expand All @@ -19,7 +18,7 @@

CGRAPH_NAMESPACE_BEGIN

static const CInt CGRAPH_CPU_NUM = (CInt)std::thread::hardware_concurrency();
static const CInt CGRAPH_CPU_NUM = static_cast<CInt>(std::thread::hardware_concurrency());
static const CInt CGRAPH_THREAD_TYPE_PRIMARY = 1;
static const CInt CGRAPH_THREAD_TYPE_SECONDARY = 2;
#ifndef _WIN32
Expand Down
Loading