Skip to content

Commit

Permalink
comments; ThreadBack: nDoneTh_
Browse files Browse the repository at this point in the history
  • Loading branch information
fchn289 committed Aug 5, 2024
1 parent 618562b commit 1a141d8
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/thread/AsyncBack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ bool AsyncBack::newTaskOK(const MT_TaskEntryFN& mt_aEntryFN, const TaskBackFN& a
fut_backFN_S_.emplace_back( // save future<> & aBackFN()
async(
launch::async,
[mt_aEntryFN]() // must cp than ref, otherwise dead loop
[mt_aEntryFN, this]() // must cp than ref, otherwise dead loop
{
const bool ret = mt_aEntryFN();
this->nDoneTh_.fetch_add(1, std::memory_order_relaxed); // fastest +1
mt_pingMainTH();
return ret;
}
Expand Down
1 change: 1 addition & 0 deletions src/thread/ThPoolBack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ThPoolBack::ThPoolBack(size_t aMaxThread)
this->taskQ_.pop_front();
}
task();
this->nDoneTh_.fetch_add(1, std::memory_order_relaxed); // fastest +1
mt_pingMainTH(); // notify mainTH 1 task done
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/thread/ThPoolBack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
//
// MT safe: NO (can be used in main thread only)
// Exception-safe: NO
// Use-safe: yes
// Use-safe: yes with condition:
// - shall not too many tasks that exceeds fut_backFN_S_/nDoneTh_/.. (impossible in most/normal cases)
// ***********************************************************************************************
#pragma once

Expand Down
11 changes: 6 additions & 5 deletions src/thread/ThreadBack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ namespace rlib
// ***********************************************************************************************
size_t ThreadBack::hdlFinishedTasks(UniLog& oneLog)
{
size_t nHandled = 0;
for (auto&& fut_backFN = fut_backFN_S_.begin(); fut_backFN != fut_backFN_S_.end();) // may limit# if too many
size_t nHandledTh = 0;
const auto nDoneTh = nDoneTh_.exchange(0, memory_order_relaxed);
for (auto&& fut_backFN = fut_backFN_S_.begin(); nHandledTh < nDoneTh && fut_backFN != fut_backFN_S_.end();)
{
// - async() failure will throw exception -> terminate since compiling forbid exception
// - valid async()'s future never invalid
// - valid packaged_task's get_future() never invalid
auto&& fut = fut_backFN->first;
// HID("(ThreadBack) nHandled=" << nHandled << ", valid=" << fut.valid() << ", backFn=" << &(fut_backFN->second));
// HID("(ThreadBack) nHandledTh=" << nHandledTh << ", valid=" << fut.valid() << ", backFn=" << &(fut_backFN->second));
if (fut.wait_for(0s) == future_status::ready)
{
fut_backFN->second(fut.get()); // callback
fut_backFN = fut_backFN_S_.erase(fut_backFN);
++nHandled;
++nHandledTh;
}
else
++fut_backFN;
} // 1 loop, simple & safe
return nHandled;
return nHandledTh;
}

// ***********************************************************************************************
Expand Down
5 changes: 4 additions & 1 deletion src/thread/ThreadBack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// ***********************************************************************************************
#pragma once

#include <atomic>
#include <functional>
#include <future>
#include <list>
Expand Down Expand Up @@ -54,12 +55,14 @@ class ThreadBack

protected:
// -------------------------------------------------------------------------------------------
StoreThreadBack fut_backFN_S_;
StoreThreadBack fut_backFN_S_; // must save future till thread end
std::atomic<size_t> nDoneTh_ = 0; // improve main thread to search done thread(s)
};

} // namespace
// ***********************************************************************************************
// YYYY-MM-DD Who v)Modification Description
// .......... ......... .......................................................................
// 2024-07-09 CSZ 1)create
// 2024-08-05 CSZ - nDoneTh_ to improve iteration of fut_backFN_S_
// ***********************************************************************************************

0 comments on commit 1a141d8

Please sign in to comment.