Skip to content

Commit 1a141d8

Browse files
committed
comments; ThreadBack: nDoneTh_
1 parent 618562b commit 1a141d8

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

src/thread/AsyncBack.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ bool AsyncBack::newTaskOK(const MT_TaskEntryFN& mt_aEntryFN, const TaskBackFN& a
2020
fut_backFN_S_.emplace_back( // save future<> & aBackFN()
2121
async(
2222
launch::async,
23-
[mt_aEntryFN]() // must cp than ref, otherwise dead loop
23+
[mt_aEntryFN, this]() // must cp than ref, otherwise dead loop
2424
{
2525
const bool ret = mt_aEntryFN();
26+
this->nDoneTh_.fetch_add(1, std::memory_order_relaxed); // fastest +1
2627
mt_pingMainTH();
2728
return ret;
2829
}

src/thread/ThPoolBack.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ThPoolBack::ThPoolBack(size_t aMaxThread)
4141
this->taskQ_.pop_front();
4242
}
4343
task();
44+
this->nDoneTh_.fetch_add(1, std::memory_order_relaxed); // fastest +1
4445
mt_pingMainTH(); // notify mainTH 1 task done
4546
}
4647
});

src/thread/ThPoolBack.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
//
1212
// MT safe: NO (can be used in main thread only)
1313
// Exception-safe: NO
14-
// Use-safe: yes
14+
// Use-safe: yes with condition:
15+
// - shall not too many tasks that exceeds fut_backFN_S_/nDoneTh_/.. (impossible in most/normal cases)
1516
// ***********************************************************************************************
1617
#pragma once
1718

src/thread/ThreadBack.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@ namespace rlib
1717
// ***********************************************************************************************
1818
size_t ThreadBack::hdlFinishedTasks(UniLog& oneLog)
1919
{
20-
size_t nHandled = 0;
21-
for (auto&& fut_backFN = fut_backFN_S_.begin(); fut_backFN != fut_backFN_S_.end();) // may limit# if too many
20+
size_t nHandledTh = 0;
21+
const auto nDoneTh = nDoneTh_.exchange(0, memory_order_relaxed);
22+
for (auto&& fut_backFN = fut_backFN_S_.begin(); nHandledTh < nDoneTh && fut_backFN != fut_backFN_S_.end();)
2223
{
2324
// - async() failure will throw exception -> terminate since compiling forbid exception
2425
// - valid async()'s future never invalid
2526
// - valid packaged_task's get_future() never invalid
2627
auto&& fut = fut_backFN->first;
27-
// HID("(ThreadBack) nHandled=" << nHandled << ", valid=" << fut.valid() << ", backFn=" << &(fut_backFN->second));
28+
// HID("(ThreadBack) nHandledTh=" << nHandledTh << ", valid=" << fut.valid() << ", backFn=" << &(fut_backFN->second));
2829
if (fut.wait_for(0s) == future_status::ready)
2930
{
3031
fut_backFN->second(fut.get()); // callback
3132
fut_backFN = fut_backFN_S_.erase(fut_backFN);
32-
++nHandled;
33+
++nHandledTh;
3334
}
3435
else
3536
++fut_backFN;
3637
} // 1 loop, simple & safe
37-
return nHandled;
38+
return nHandledTh;
3839
}
3940

4041
// ***********************************************************************************************

src/thread/ThreadBack.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// ***********************************************************************************************
1111
#pragma once
1212

13+
#include <atomic>
1314
#include <functional>
1415
#include <future>
1516
#include <list>
@@ -54,12 +55,14 @@ class ThreadBack
5455

5556
protected:
5657
// -------------------------------------------------------------------------------------------
57-
StoreThreadBack fut_backFN_S_;
58+
StoreThreadBack fut_backFN_S_; // must save future till thread end
59+
std::atomic<size_t> nDoneTh_ = 0; // improve main thread to search done thread(s)
5860
};
5961

6062
} // namespace
6163
// ***********************************************************************************************
6264
// YYYY-MM-DD Who v)Modification Description
6365
// .......... ......... .......................................................................
6466
// 2024-07-09 CSZ 1)create
67+
// 2024-08-05 CSZ - nDoneTh_ to improve iteration of fut_backFN_S_
6568
// ***********************************************************************************************

0 commit comments

Comments
 (0)