Skip to content

Commit

Permalink
MtInQueue: template pop() for convenience
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Sep 6, 2023
1 parent 80df987 commit b924fa7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
19 changes: 0 additions & 19 deletions src/queue/MtInQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ size_t MtInQueue::mt_clear()
return sizeQueue + sizeCache;
}

// ***********************************************************************************************
shared_ptr<void> MtInQueue::pop()
{
if (cache_.empty())
{
unique_lock<mutex> guard(mutex_, try_to_lock); // avoid block main thread
if (! guard.owns_lock()) // avoid block main thread
return nullptr;
if (queue_.empty())
return nullptr;
cache_.swap(queue_); // fast & for at most ele
}
// unlocked

auto ele = cache_.front();
cache_.pop_front();
return ele;
}

// ***********************************************************************************************
void MtInQueue::mt_push(shared_ptr<void> aEle)
{
Expand Down
24 changes: 23 additions & 1 deletion src/queue/MtInQueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class MtInQueue
{
public:
void mt_push(shared_ptr<void> aEle);
shared_ptr<void> pop(); // shall access in main thread ONLY!!! high performance

template<class aEleType = void> // convenient usr
shared_ptr<aEleType> pop(); // shall access in main thread ONLY!!! high performance

size_t mt_size();
size_t mt_clear();
Expand All @@ -51,6 +53,26 @@ class MtInQueue
mutex& backdoor() { return mutex_; }
#endif
};

// ***********************************************************************************************
template<class aEleType>
shared_ptr<aEleType> MtInQueue::pop()
{
if (cache_.empty())
{
unique_lock<mutex> guard(mutex_, try_to_lock); // avoid block main thread
if (! guard.owns_lock()) // avoid block main thread
return nullptr;
if (queue_.empty())
return nullptr;
cache_.swap(queue_); // fast & for at most ele
}
// unlocked

auto ele = cache_.front();
cache_.pop_front();
return static_pointer_cast<aEleType>(ele);
}
} // namespace
// ***********************************************************************************************
// YYYY-MM-DD Who v)Modification Description
Expand Down
6 changes: 3 additions & 3 deletions ut/queue/MtInQueueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ TEST_F(MtInQueueTest, GOLD_fifo_multiThreadSafe)
INF("GOLD_fifo_multiThreadSafe: before loop")
while (nHdl < nMsg)
{
auto msg = static_pointer_cast<int>(mtQ_.pop());
auto msg = (mtQ_.pop<int>());
if (msg) ASSERT_EQ(nHdl++, *msg) << "REQ: fifo";
else this_thread::yield(); // simulate real world
}
Expand All @@ -71,10 +71,10 @@ TEST_F(MtInQueueTest, GOLD_nonBlock_pop)
ASSERT_FALSE(mtQ_.pop()) << "REQ: not blocked" << endl;

mtQ_.backdoor().unlock();
ASSERT_EQ("1st", *static_pointer_cast<string>(mtQ_.pop())) << "REQ: can pop";
ASSERT_EQ("1st", *(mtQ_.pop<string>())) << "REQ: can pop";

mtQ_.backdoor().lock();
ASSERT_EQ("2nd", *static_pointer_cast<string>(mtQ_.pop())) << "REQ: can pop from cache" << endl;
ASSERT_EQ("2nd", *(mtQ_.pop<string>())) << "REQ: can pop from cache" << endl;
mtQ_.backdoor().unlock();
}
TEST_F(MtInQueueTest, size)
Expand Down

0 comments on commit b924fa7

Please sign in to comment.