Skip to content

Commit f93e941

Browse files
committed
except: lambda, comment, history
1 parent a4208aa commit f93e941

File tree

8 files changed

+16
-27
lines changed

8 files changed

+16
-27
lines changed

src/safe_mem/SafePtr.hpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
// . under single thread
2020
// . so eg after MtInQueue.mt_push(), shall NOT touch pushed SafePtr
2121
// . only HID is MT safe that can be used here
22-
// . no exception
2322
// . T's duty to ensure it's inner safety (eg no exception from T's constructor)
2423
// . loop-ref: out-duty, usr issue than SafePtr
2524
// . safe ptr array: no need since std::array
@@ -185,7 +184,7 @@ template<typename U, typename... ConstructArgs>
185184
SafePtr<U> make_safe(ConstructArgs&&... aArgs) noexcept
186185
{
187186
SafePtr<U> safeU;
188-
try {
187+
try { // bad_alloc, or except from U's constructor
189188
safeU.pT_ = std::make_shared<U>(std::forward<ConstructArgs>(aArgs)...); // std::make_shared, not boost's
190189
// HID("new ptr=" << (void*)(safeU.pT_.get())); // too many print; void* print addr rather than content(dangeous)
191190
} catch(...) {
@@ -259,14 +258,9 @@ SafeWeak<T>::SafeWeak(const SafePtr<T>& aSafeFrom) noexcept
259258
template<typename T>
260259
SafePtr<T> SafeWeak<T>::lock() const noexcept
261260
{
262-
SafePtr<T> ret;
263-
if (pT_.expired())
264-
return ret;
265-
266-
ret.pT_ = pT_.lock();
267-
ret.realType_ = realType_;
268-
ret.lastType_ = lastType_;
269-
return ret;
261+
return (pT_.expired())
262+
? nullptr
263+
: SafePtr<T>(pT_.lock(), realType_, lastType_); // constructor is faster
270264
}
271265
} // namespace
272266

@@ -289,7 +283,7 @@ struct std::hash<rlib::SafePtr<T>>
289283
// 2024-06-28 CSZ - dynamic_pointer_cast ok or ret null
290284
// 2024-10-16 CSZ - dynamic_pointer_cast to safe_cast since std not allowed
291285
// 2025-02-13 CSZ 4)SafeWeak
292-
// 2025-03-24 CSZ 5)enable exception, tolerate except is safer
286+
// 2025-03-24 CSZ 5)enable exception: tolerate except is safer; can't recover except->terminate
293287
// ***********************************************************************************************
294288
// - Q&A
295289
// . How to solve safety issue:

src/thread/AsyncBack.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ bool AsyncBack::newTaskOK(const MT_TaskEntryFN& mt_aEntryFN, const TaskBackFN& a
1818
return false;
1919

2020
// create new thread
21-
// - ensure AsyncBack alive when thread alive - yes since ~AsyncBack() will wait all fut over
21+
// - impossible thread alive but AsyncBack destructed, since ~AsyncBack() will wait all fut over
2222
auto fut = async(
2323
launch::async,
2424
// - must cp mt_aEntryFN than ref, otherwise dead loop
2525
// - &mt_nDoneFut is better than "this" that can access other non-MT-safe member
26-
[mt_aEntryFN, &mt_nDoneFut = mt_nDoneFut_]() // thread main
26+
[mt_aEntryFN, &mt_nDoneFut = mt_nDoneFut_]() noexcept // thread main
2727
{
2828
SafePtr ret;
2929
try { ret = mt_aEntryFN(); }

src/thread/AsyncBack.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,8 @@
4242
// . no duty to any unsafe behavior of MT_TaskEntryFN & TaskBackFN
4343
// - MT safe:
4444
// . MT_/mt_ prefix: yes
45-
// . others: NO (only use in main thread - most dom lib code shall in main thread - simple & nsesilbe)
46-
// . AsyncBack can call mt_inMyMainTH() to ensure this
47-
// . but can all rlib func call mt_inMyMainTH()? little benefit so giveup
48-
// * common sense/principle: rlib (include AsyncBack) not call mt_inMyMainTH()
49-
// . mt_inMyMainTH() for user debug - any main-thread func shall ret T if call mt_inMyMainTH()
50-
// - Exception-safe: follow noexcept-declare
45+
// . others: NO (only in main thread - most dom lib code shall be in main thread - simple)
46+
// . mt_inMyMainTH() for user debug - any main-thread func shall ret true if call mt_inMyMainTH()
5147
// ***********************************************************************************************
5248
#pragma once
5349

@@ -77,7 +73,7 @@ class AsyncBack : public ThreadBack
7773
// 2023-09-14 CSZ 2)align with MsgSelf
7874
// 2023-10-25 CSZ - with semaphore's wait-notify
7975
// 2024-07-10 CSZ - mv common to base=ThreadBack
80-
// 2025-03-21 CSZ 3)enhance to avoid hang; enable exception for safety
76+
// 2025-03-21 CSZ 3)enable exception: tolerate except is safer; can't recover except->terminate
8177
// ***********************************************************************************************
8278
// - Q&A:
8379
// . MT_/mt_ prefix

src/thread/MtInQueue.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,5 @@ MtInQueue& mt_getQ() noexcept;
175175
// 2024-02-15 CSZ 3)use SafePtr (mem-safe); shared_ptr is not mem-safe
176176
// 2024-03-10 CSZ - enhance safe of setHdlrOK()
177177
// 2024-10-05 CSZ - integrate with domino (giveup since multi-same-type)
178-
// 2025-03-24 CSZ 4)enable exception: tolerate except is safer; can't recover->terminate
178+
// 2025-03-24 CSZ 4)enable exception: tolerate except is safer; can't recover except->terminate
179179
// ***********************************************************************************************

src/thread/ThPoolBack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ThPoolBack::ThPoolBack(size_t aMaxThread)
2525
thPool_.reserve(aMaxThread); // not construct any thread
2626
for (size_t i = 0; i < aMaxThread; ++i)
2727
{
28-
thread th([this]
28+
thread th([this]() noexcept
2929
{ // thread main()
3030
for (;;)
3131
{

src/thread/ThPoolBack.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
// - MT safe:
1616
// . MT_/mt_ prefix: yes
1717
// . others: no (must call in main thread)
18-
// - Exception-safe: follow noexcept-declare
1918
// ***********************************************************************************************
2019
#pragma once
2120

@@ -36,7 +35,7 @@ class ThPoolBack : public ThreadBack
3635
public:
3736
// @brief Constructs a thread pool with exactly the specified number of threads.
3837
// @param aMaxThread: Exact number of threads to create (minimum 1).
39-
// @throws runtime_error: If any thread cannot be created, ensuring the pool matches the requested size.
38+
// @except if any thread cannot be created/joinable
4039
explicit ThPoolBack(size_t aMaxThread = 10) noexcept(false);
4140
~ThPoolBack() noexcept;
4241

@@ -59,5 +58,5 @@ class ThPoolBack : public ThreadBack
5958
// YYYY-MM-DD Who v)Modification Description
6059
// .......... ......... .......................................................................
6160
// 2024-07-09 CSZ 1)create
62-
// 2025-03-21 CSZ 2)enhance to avoid hang; enable exception for safety
61+
// 2025-03-21 CSZ 2)enable exception: tolerate except is safer; can't recover except->terminate
6362
// ***********************************************************************************************

src/thread/ThreadBack.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class ThreadBack
7373
// 2024-07-09 CSZ 1)create
7474
// 2024-08-05 CSZ - mt_nDoneFut_ to improve iteration of fut_backFN_S_
7575
// - MT_TaskEntryFN ret SafePtr<void> instead of bool
76-
// 2025-03-21 CSZ 2)enhance to avoid hang; enable exception for safety
76+
// 2025-03-21 CSZ 2)enable exception: tolerate except is safer; can't recover except->terminate
7777
// ***********************************************************************************************
7878
// - why SafePtr
7979
// . thread can ret any type data include bool

src/thread/ThreadBackViaMsgSelf.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ TaskBackFN viaMsgSelf(const TaskBackFN& aBackFN, EMsgPriority aPri = EMsgPri_NOR
2828
auto&& msgSelf = MSG_SELF;
2929
return ! aBackFN || msgSelf.get() == nullptr
3030
? TaskBackFN() // empty fn
31-
: [aBackFN, msgSelf, aPri](SafePtr<void> aRet) // must cp aBackFN since lambda run later in diff lifecycle
31+
: [aBackFN, msgSelf, aPri](SafePtr<void> aRet) noexcept // must cp aBackFN since lambda run later in diff lifecycle
3232
{
3333
msgSelf->newMsg(bind(aBackFN, aRet), aPri); // wrap aBackFN to queue in MsgSelf
3434
};

0 commit comments

Comments
 (0)