From f5858421f768fce4fe08aa0cb1d24bf8bbdeaee5 Mon Sep 17 00:00:00 2001 From: lct1001 <2739960959@qq.com> Date: Wed, 29 Oct 2025 21:05:03 +0800 Subject: [PATCH 1/4] components:libc:cplusplus:os/utest:Some comments have been initially added. Some comments have been initially added. components/libc/cplusplus/os/cxx_Semaphore.cpp components/libc/cplusplus/os/cxx_Thread.cpp components/libc/cplusplus/utest/tc_atomic.cpp components/libc/cplusplus/utest/tc_smartptr.cpp components/libc/cplusplus/utest/tc_thread.cpp Signed-off-by:Liu Chengtao<2739960959@qq.com> --- .../libc/cplusplus/os/cxx_Semaphore.cpp | 17 ++++++++ components/libc/cplusplus/os/cxx_Thread.cpp | 39 ++++++++++++++++++ components/libc/cplusplus/utest/tc_atomic.cpp | 3 +- .../libc/cplusplus/utest/tc_smartptr.cpp | 6 +++ components/libc/cplusplus/utest/tc_thread.cpp | 41 +++++++++++++++---- 5 files changed, 96 insertions(+), 10 deletions(-) diff --git a/components/libc/cplusplus/os/cxx_Semaphore.cpp b/components/libc/cplusplus/os/cxx_Semaphore.cpp index 672eeb04afe..d3e198716d4 100644 --- a/components/libc/cplusplus/os/cxx_Semaphore.cpp +++ b/components/libc/cplusplus/os/cxx_Semaphore.cpp @@ -11,28 +11,45 @@ using namespace rtthread; +/** + * @brief Semaphore class implementation. + */ Semaphore::Semaphore(const char *name, int32_t count) { + // Initialize the semaphore with a specified count and FIFO order. rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO); } +/** + * @brief Wait on the semaphore. + * @param millisec Timeout in milliseconds. + * @return Boolean indicating if the semaphore was successfully taken. + */ bool Semaphore::wait(int32_t millisec) { rt_int32_t tick; + // Convert milliseconds to system ticks. if (millisec < 0) tick = -1; else tick = rt_tick_from_millisecond(millisec); + // Attempt to take the semaphore. return rt_sem_take(&mID, tick) == RT_EOK; } +/** + * @brief Release the semaphore. + */ void Semaphore::release(void) { rt_sem_release(&mID); } +/** + * Detach the semaphore when the object is destroyed. + */ Semaphore::~Semaphore() { rt_sem_detach(&mID); diff --git a/components/libc/cplusplus/os/cxx_Thread.cpp b/components/libc/cplusplus/os/cxx_Thread.cpp index f3484bb1f49..026a187e381 100644 --- a/components/libc/cplusplus/os/cxx_Thread.cpp +++ b/components/libc/cplusplus/os/cxx_Thread.cpp @@ -11,6 +11,9 @@ using namespace rtthread; +/** + * @brief Thread class constructor with parameters for stack size, priority, tick, and name. + */ Thread::Thread(rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick, @@ -27,6 +30,9 @@ Thread::Thread(rt_uint32_t stack_size, tick); } +/** + * @brief Thread class constructor with entry function and parameters. + */ Thread::Thread(void (*entry)(void *p), void *p, rt_uint32_t stack_size, @@ -45,12 +51,19 @@ Thread::Thread(void (*entry)(void *p), tick); } +/** + * @brief Detach the event and delete the thread when the object is destroyed. + */ Thread::~Thread() { rt_event_detach(&_event); rt_thread_delete(_thread); } +/** + * @brief Start the thread execution. + * @return Boolean indicating if the thread was successfully started. + */ bool Thread::start() { if (rt_thread_startup(_thread) == RT_EOK) @@ -61,20 +74,30 @@ bool Thread::start() return started; } +/** + * Make the thread sleep for a specified duration. + * @param millisec Duration in milliseconds. + */ void Thread::sleep(int32_t millisec) { rt_int32_t tick; + // Convert milliseconds to system ticks. if (millisec < 0) tick = 1; else tick = rt_tick_from_millisecond(millisec); + // Delay the thread for a specified number of ticks. rt_thread_delay(tick); } +/** + * Static function to run the thread's entry function. + */ void Thread::func(Thread *pThis) { + // If an entry function is provided, execute it. if (pThis->_entry != RT_NULL) { pThis->_entry(pThis->_param); @@ -84,30 +107,46 @@ void Thread::func(Thread *pThis) pThis->run(pThis->_param); } + // Send an event to signal thread completion. rt_event_send(&pThis->_event, 1); } +/** + * Default run function that can be overridden by subclasses. + */ void Thread::run(void *parameter) { /* please overload this method */ } +/** + * Wait for the thread to complete with a timeout. + * @param millisec Timeout in milliseconds. + * @return Status code indicating the execution status. + */ rt_err_t Thread::wait(int32_t millisec) { return join(millisec); } +/** + * Join the thread with a timeout. + * @param millisec Timeout in milliseconds. + * @return Status code indicating the execution status. + */ rt_err_t Thread::join(int32_t millisec) { if (started) { rt_int32_t tick; + // Convert milliseconds to system ticks. if (millisec < 0) tick = -1; else tick = rt_tick_from_millisecond(millisec); + // Wait for the thread's completion event. return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL); } else diff --git a/components/libc/cplusplus/utest/tc_atomic.cpp b/components/libc/cplusplus/utest/tc_atomic.cpp index 8b5bdbd9bad..956d53e4674 100644 --- a/components/libc/cplusplus/utest/tc_atomic.cpp +++ b/components/libc/cplusplus/utest/tc_atomic.cpp @@ -238,12 +238,13 @@ static void test_compare_exchange_weak(void) } uassert_int_equal(val.load(), 2); } - +// Test case initialization function. static rt_err_t utest_tc_init(void) { return RT_EOK; } +// Test case cleanup function. static rt_err_t utest_tc_cleanup(void) { return RT_EOK; diff --git a/components/libc/cplusplus/utest/tc_smartptr.cpp b/components/libc/cplusplus/utest/tc_smartptr.cpp index 1a9fa7eb116..977d12fe5ab 100644 --- a/components/libc/cplusplus/utest/tc_smartptr.cpp +++ b/components/libc/cplusplus/utest/tc_smartptr.cpp @@ -35,10 +35,16 @@ static void test_shared_ptr(void) uassert_int_equal(p1.use_count(), 2); } +/** + * @brief Test case initialization function. + */ static rt_err_t utest_tc_init(void) { return RT_EOK; } +/** + * @brief Test case cleanup function. + */ static rt_err_t utest_tc_cleanup(void) { diff --git a/components/libc/cplusplus/utest/tc_thread.cpp b/components/libc/cplusplus/utest/tc_thread.cpp index c60a9ab6025..5767694d368 100644 --- a/components/libc/cplusplus/utest/tc_thread.cpp +++ b/components/libc/cplusplus/utest/tc_thread.cpp @@ -12,48 +12,71 @@ #include "utest.h" #include + +/** + * @brief Function to test thread functionality. + */ static void test_thread(void) { - int count = 0; + int count = 0; // Initialize a counter to zero. + + // Lambda function to increment the count. auto func = [&]() mutable { for (int i = 0; i < 100; ++i) { - ++count; + ++count; // Increment the count 100 times. } }; + // Create and run a thread executing the lambda function. std::thread t1(func); - t1.join(); + t1.join(); // Wait for the thread to finish execution. + // Verify if the count is as expected after the first thread execution. if (count != 100) { - uassert_false(1); + uassert_false(1); // Assert failure if count is not 100. } + // Create and run another thread executing the same lambda function. std::thread t2(func); - t2.join(); + t2.join(); // Wait for the second thread to finish execution. + // Verify if the count is as expected after the second thread execution. if (count != 200) { - uassert_false(1); + uassert_false(1); // Assert failure if count is not 200. } + // If both assertions passed, the test is successful. uassert_true(1); } + +/** + * @brief Test case initialization function. + */ + static rt_err_t utest_tc_init(void) { - return RT_EOK; + return RT_EOK; } - +/** + * @brief Test case cleanup function. + */ static rt_err_t utest_tc_cleanup(void) { - return RT_EOK; + return RT_EOK; } +/** + * @brief Main test case function that runs the test. + */ static void testcase(void) { UTEST_UNIT_RUN(test_thread); } + +// Export the test case with initialization and cleanup functions and a timeout of 10 ticks. UTEST_TC_EXPORT(testcase, "components.libc.cpp.thread_tc", utest_tc_init, utest_tc_cleanup, 10); From bfe680c913d73c4558d8c74499cdb1ecc44bc05f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 29 Oct 2025 13:50:05 +0000 Subject: [PATCH 2/4] style: format code with clang-format [skip ci] --- components/libc/cplusplus/os/cxx_Thread.cpp | 12 +++++----- components/libc/cplusplus/utest/tc_atomic.cpp | 24 +++++++++---------- components/libc/cplusplus/utest/tc_thread.cpp | 9 ++++--- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/components/libc/cplusplus/os/cxx_Thread.cpp b/components/libc/cplusplus/os/cxx_Thread.cpp index 026a187e381..362215cce17 100644 --- a/components/libc/cplusplus/os/cxx_Thread.cpp +++ b/components/libc/cplusplus/os/cxx_Thread.cpp @@ -15,10 +15,10 @@ using namespace rtthread; * @brief Thread class constructor with parameters for stack size, priority, tick, and name. */ Thread::Thread(rt_uint32_t stack_size, - rt_uint8_t priority, + rt_uint8_t priority, rt_uint32_t tick, - const char *name) - : _entry(RT_NULL), _param(RT_NULL), started(false) + const char *name) : + _entry(RT_NULL), _param(RT_NULL), started(false) { rt_event_init(&_event, name, 0); @@ -36,10 +36,10 @@ Thread::Thread(rt_uint32_t stack_size, Thread::Thread(void (*entry)(void *p), void *p, rt_uint32_t stack_size, - rt_uint8_t priority, + rt_uint8_t priority, rt_uint32_t tick, - const char *name) - : _entry(entry), _param(p), started(false) + const char *name) : + _entry(entry), _param(p), started(false) { rt_event_init(&_event, name, 0); diff --git a/components/libc/cplusplus/utest/tc_atomic.cpp b/components/libc/cplusplus/utest/tc_atomic.cpp index 956d53e4674..6ff725dd9fc 100644 --- a/components/libc/cplusplus/utest/tc_atomic.cpp +++ b/components/libc/cplusplus/utest/tc_atomic.cpp @@ -20,7 +20,7 @@ */ static void test_atomic_load_store_int32(void) { - constexpr int kRound = 10000000; + constexpr int kRound = 10000000; std::atomic thread_count(0); std::atomic count(100); uassert_int_equal(count.load(), 100); @@ -56,7 +56,7 @@ static void test_atomic_load_store_int32(void) */ static void test_atomic_load_store_int64(void) { - constexpr int kRound = 10000000; + constexpr int kRound = 10000000; std::atomic thread_count(0); std::atomic count(100); uassert_int_equal(count.load(), 100); @@ -121,9 +121,9 @@ static void test_atomic_basic_int32(void) val.exchange(1); uassert_int_equal(val.load(), 1); - int32_t x = 2; - int32_t y = 3; - bool exchanged = val.compare_exchange_strong(x, y); + int32_t x = 2; + int32_t y = 3; + bool exchanged = val.compare_exchange_strong(x, y); uassert_false(exchanged); uassert_int_equal(val.load(), 1); uassert_int_equal(x, 1); @@ -168,9 +168,9 @@ static void test_atomic_basic_int64(void) val.exchange(1); uassert_int_equal(val.load(), 1); - int64_t x = 2; - int64_t y = 3; - bool exchanged = val.compare_exchange_strong(x, y); + int64_t x = 2; + int64_t y = 3; + bool exchanged = val.compare_exchange_strong(x, y); uassert_false(exchanged); uassert_int_equal(val.load(), 1); uassert_int_equal(x, 1); @@ -191,7 +191,7 @@ static void test_atomic_bool(void) flag.exchange(false); uassert_false(flag.load()); bool expected = false; - bool desired = true; + bool desired = true; uassert_true(flag.compare_exchange_strong(expected, desired)); uassert_true(flag.load()); } @@ -201,7 +201,7 @@ static void test_atomic_bool(void) */ static void test_atomic_pointer(void) { - int a = 1, b = 2; + int a = 1, b = 2; std::atomic ptr(&a); ptr.store(&b); uassert_int_equal(*ptr.load(), 2); @@ -230,8 +230,8 @@ static void test_memory_order(void) static void test_compare_exchange_weak(void) { std::atomic val(1); - int expected = 1; - int desired = 2; + int expected = 1; + int desired = 2; while (!val.compare_exchange_weak(expected, desired)) { expected = 1; // reset diff --git a/components/libc/cplusplus/utest/tc_thread.cpp b/components/libc/cplusplus/utest/tc_thread.cpp index 5767694d368..755073e8e7b 100644 --- a/components/libc/cplusplus/utest/tc_thread.cpp +++ b/components/libc/cplusplus/utest/tc_thread.cpp @@ -21,8 +21,7 @@ static void test_thread(void) int count = 0; // Initialize a counter to zero. // Lambda function to increment the count. - auto func = [&]() mutable - { + auto func = [&]() mutable { for (int i = 0; i < 100; ++i) { ++count; // Increment the count 100 times. @@ -60,19 +59,19 @@ static void test_thread(void) static rt_err_t utest_tc_init(void) { - return RT_EOK; + return RT_EOK; } /** * @brief Test case cleanup function. */ static rt_err_t utest_tc_cleanup(void) { - return RT_EOK; + return RT_EOK; } /** * @brief Main test case function that runs the test. - */ + */ static void testcase(void) { UTEST_UNIT_RUN(test_thread); From eb1d86a61ac5d78c44bfe343c190565a6f261093 Mon Sep 17 00:00:00 2001 From: lct1001 <2739960959@qq.com> Date: Thu, 13 Nov 2025 18:11:15 +0800 Subject: [PATCH 3/4] components:libc:cplusplus:Adjust the annotation to fit the rtthread specification Modify the os:cxx_Semaphore.cpp and cxx_Thread.cpp to comply with the rtthread specification. Change the comment symbol in utest:tc_thread.cpp from '//' to '/**/' to comply with rtthread specifications.For utest:tc_atomic.cpp and tc_smartptr.cpp,select the original respository files without modification. signed-off-by: Liu Chengtao<2739960959@qq.com> --- .../libc/cplusplus/os/cxx_Semaphore.cpp | 11 +++--- components/libc/cplusplus/os/cxx_Thread.cpp | 31 +++++++++------- components/libc/cplusplus/utest/tc_atomic.cpp | 26 ++++++------- .../libc/cplusplus/utest/tc_smartptr.cpp | 4 +- components/libc/cplusplus/utest/tc_thread.cpp | 37 +++++++++---------- 5 files changed, 55 insertions(+), 54 deletions(-) diff --git a/components/libc/cplusplus/os/cxx_Semaphore.cpp b/components/libc/cplusplus/os/cxx_Semaphore.cpp index d3e198716d4..6e1d4b2adb2 100644 --- a/components/libc/cplusplus/os/cxx_Semaphore.cpp +++ b/components/libc/cplusplus/os/cxx_Semaphore.cpp @@ -13,29 +13,28 @@ using namespace rtthread; /** * @brief Semaphore class implementation. + * @param name Semaphore name + * @param count Initial semaphore count */ Semaphore::Semaphore(const char *name, int32_t count) { - // Initialize the semaphore with a specified count and FIFO order. rt_sem_init(&mID, name, count, RT_IPC_FLAG_FIFO); } /** * @brief Wait on the semaphore. - * @param millisec Timeout in milliseconds. - * @return Boolean indicating if the semaphore was successfully taken. + * @param millisec Timeout in milliseconds (-1 for infinite wait). + * @return true if the semaphore was successfully taken, false on timeout. */ bool Semaphore::wait(int32_t millisec) { rt_int32_t tick; - // Convert milliseconds to system ticks. if (millisec < 0) tick = -1; else tick = rt_tick_from_millisecond(millisec); - // Attempt to take the semaphore. return rt_sem_take(&mID, tick) == RT_EOK; } @@ -48,7 +47,7 @@ void Semaphore::release(void) } /** - * Detach the semaphore when the object is destroyed. + * @brief Detach the semaphore when the object is destroyed. */ Semaphore::~Semaphore() { diff --git a/components/libc/cplusplus/os/cxx_Thread.cpp b/components/libc/cplusplus/os/cxx_Thread.cpp index 026a187e381..90c1d274eea 100644 --- a/components/libc/cplusplus/os/cxx_Thread.cpp +++ b/components/libc/cplusplus/os/cxx_Thread.cpp @@ -13,6 +13,10 @@ using namespace rtthread; /** * @brief Thread class constructor with parameters for stack size, priority, tick, and name. + * @param stack_size Stack size in bytes + * @param priority Thread priority + * @param tick Time slice in ticks + * @param name Thread name */ Thread::Thread(rt_uint32_t stack_size, rt_uint8_t priority, @@ -32,6 +36,12 @@ Thread::Thread(rt_uint32_t stack_size, /** * @brief Thread class constructor with entry function and parameters. + * @param entry The entry function pointer for the thread. + * @param p The parameter to pass to the entry function. + * @param stack_size The size of the thread stack in bytes. + * @param priority The priority of the thread. + * @param tick The time slice (tick) for the thread. + * @param name The name of the thread. */ Thread::Thread(void (*entry)(void *p), void *p, @@ -62,7 +72,7 @@ Thread::~Thread() /** * @brief Start the thread execution. - * @return Boolean indicating if the thread was successfully started. + * @return true if the thread was successfully started. */ bool Thread::start() { @@ -75,29 +85,26 @@ bool Thread::start() } /** - * Make the thread sleep for a specified duration. + * @brief Make the thread sleep for a specified duration. * @param millisec Duration in milliseconds. */ void Thread::sleep(int32_t millisec) { rt_int32_t tick; - // Convert milliseconds to system ticks. if (millisec < 0) tick = 1; else tick = rt_tick_from_millisecond(millisec); - // Delay the thread for a specified number of ticks. rt_thread_delay(tick); } /** - * Static function to run the thread's entry function. + * @brief function to run the thread's entry function. */ void Thread::func(Thread *pThis) { - // If an entry function is provided, execute it. if (pThis->_entry != RT_NULL) { pThis->_entry(pThis->_param); @@ -107,12 +114,11 @@ void Thread::func(Thread *pThis) pThis->run(pThis->_param); } - // Send an event to signal thread completion. rt_event_send(&pThis->_event, 1); } /** - * Default run function that can be overridden by subclasses. + * @brief Default run function that can be overridden by subclasses. */ void Thread::run(void *parameter) { @@ -120,9 +126,9 @@ void Thread::run(void *parameter) } /** - * Wait for the thread to complete with a timeout. + * @brief Wait for the thread to complete with a timeout. * @param millisec Timeout in milliseconds. - * @return Status code indicating the execution status. + * @return RT_EOK if the thread completed within the timeout, error code otherwise. */ rt_err_t Thread::wait(int32_t millisec) { @@ -130,7 +136,7 @@ rt_err_t Thread::wait(int32_t millisec) } /** - * Join the thread with a timeout. + * @brief the thread with a timeout. * @param millisec Timeout in milliseconds. * @return Status code indicating the execution status. */ @@ -139,14 +145,11 @@ rt_err_t Thread::join(int32_t millisec) if (started) { rt_int32_t tick; - - // Convert milliseconds to system ticks. if (millisec < 0) tick = -1; else tick = rt_tick_from_millisecond(millisec); - // Wait for the thread's completion event. return rt_event_recv(&_event, 1, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, tick, RT_NULL); } else diff --git a/components/libc/cplusplus/utest/tc_atomic.cpp b/components/libc/cplusplus/utest/tc_atomic.cpp index 956d53e4674..919c0aa3112 100644 --- a/components/libc/cplusplus/utest/tc_atomic.cpp +++ b/components/libc/cplusplus/utest/tc_atomic.cpp @@ -20,7 +20,7 @@ */ static void test_atomic_load_store_int32(void) { - constexpr int kRound = 10000000; + constexpr int kRound = 10000000; std::atomic thread_count(0); std::atomic count(100); uassert_int_equal(count.load(), 100); @@ -56,7 +56,7 @@ static void test_atomic_load_store_int32(void) */ static void test_atomic_load_store_int64(void) { - constexpr int kRound = 10000000; + constexpr int kRound = 10000000; std::atomic thread_count(0); std::atomic count(100); uassert_int_equal(count.load(), 100); @@ -121,9 +121,9 @@ static void test_atomic_basic_int32(void) val.exchange(1); uassert_int_equal(val.load(), 1); - int32_t x = 2; - int32_t y = 3; - bool exchanged = val.compare_exchange_strong(x, y); + int32_t x = 2; + int32_t y = 3; + bool exchanged = val.compare_exchange_strong(x, y); uassert_false(exchanged); uassert_int_equal(val.load(), 1); uassert_int_equal(x, 1); @@ -168,9 +168,9 @@ static void test_atomic_basic_int64(void) val.exchange(1); uassert_int_equal(val.load(), 1); - int64_t x = 2; - int64_t y = 3; - bool exchanged = val.compare_exchange_strong(x, y); + int64_t x = 2; + int64_t y = 3; + bool exchanged = val.compare_exchange_strong(x, y); uassert_false(exchanged); uassert_int_equal(val.load(), 1); uassert_int_equal(x, 1); @@ -191,7 +191,7 @@ static void test_atomic_bool(void) flag.exchange(false); uassert_false(flag.load()); bool expected = false; - bool desired = true; + bool desired = true; uassert_true(flag.compare_exchange_strong(expected, desired)); uassert_true(flag.load()); } @@ -201,7 +201,7 @@ static void test_atomic_bool(void) */ static void test_atomic_pointer(void) { - int a = 1, b = 2; + int a = 1, b = 2; std::atomic ptr(&a); ptr.store(&b); uassert_int_equal(*ptr.load(), 2); @@ -230,8 +230,8 @@ static void test_memory_order(void) static void test_compare_exchange_weak(void) { std::atomic val(1); - int expected = 1; - int desired = 2; + int expected = 1; + int desired = 2; while (!val.compare_exchange_weak(expected, desired)) { expected = 1; // reset @@ -269,4 +269,4 @@ static void testcase(void) /* Test compare_exchange_weak operation */ UTEST_UNIT_RUN(test_compare_exchange_weak); } -UTEST_TC_EXPORT(testcase, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10); +UTEST_TC_EXPORT(testcase, "components.libc.cpp.atomic_tc", utest_tc_init, utest_tc_cleanup, 10); \ No newline at end of file diff --git a/components/libc/cplusplus/utest/tc_smartptr.cpp b/components/libc/cplusplus/utest/tc_smartptr.cpp index 977d12fe5ab..0325ddbab23 100644 --- a/components/libc/cplusplus/utest/tc_smartptr.cpp +++ b/components/libc/cplusplus/utest/tc_smartptr.cpp @@ -42,10 +42,10 @@ static rt_err_t utest_tc_init(void) { return RT_EOK; } + /** * @brief Test case cleanup function. */ - static rt_err_t utest_tc_cleanup(void) { return RT_EOK; @@ -58,4 +58,4 @@ static void testcase(void) /* Test shared_ptr basic operations */ UTEST_UNIT_RUN(test_shared_ptr); } -UTEST_TC_EXPORT(testcase, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10); +UTEST_TC_EXPORT(testcase, "components.libc.cpp.smartptr_tc", utest_tc_init, utest_tc_cleanup, 10); \ No newline at end of file diff --git a/components/libc/cplusplus/utest/tc_thread.cpp b/components/libc/cplusplus/utest/tc_thread.cpp index 5767694d368..30d9de2dee4 100644 --- a/components/libc/cplusplus/utest/tc_thread.cpp +++ b/components/libc/cplusplus/utest/tc_thread.cpp @@ -18,38 +18,37 @@ */ static void test_thread(void) { - int count = 0; // Initialize a counter to zero. + int count = 0; - // Lambda function to increment the count. - auto func = [&]() mutable - { + /* Lambda function to increment the count.*/ + auto func = [&]() mutable { for (int i = 0; i < 100; ++i) { - ++count; // Increment the count 100 times. + ++count; } }; - // Create and run a thread executing the lambda function. + /* Create and run a thread executing the lambda function. */ std::thread t1(func); - t1.join(); // Wait for the thread to finish execution. + /* Wait for the thread to finish execution.*/ + t1.join(); - // Verify if the count is as expected after the first thread execution. + /* Verify if the count is as expected after the first thread execution.*/ if (count != 100) { - uassert_false(1); // Assert failure if count is not 100. + uassert_false(1); } - // Create and run another thread executing the same lambda function. + /* Create and run another thread executing the same lambda function. */ std::thread t2(func); - t2.join(); // Wait for the second thread to finish execution. + t2.join(); - // Verify if the count is as expected after the second thread execution. if (count != 200) { - uassert_false(1); // Assert failure if count is not 200. + uassert_false(1); } - // If both assertions passed, the test is successful. + /* If both assertions passed, the test is successful.*/ uassert_true(1); } @@ -60,23 +59,23 @@ static void test_thread(void) static rt_err_t utest_tc_init(void) { - return RT_EOK; + return RT_EOK; } /** * @brief Test case cleanup function. */ static rt_err_t utest_tc_cleanup(void) { - return RT_EOK; + return RT_EOK; } /** * @brief Main test case function that runs the test. - */ + */ static void testcase(void) { UTEST_UNIT_RUN(test_thread); } -// Export the test case with initialization and cleanup functions and a timeout of 10 ticks. -UTEST_TC_EXPORT(testcase, "components.libc.cpp.thread_tc", utest_tc_init, utest_tc_cleanup, 10); +/* Export the test case with initialization and cleanup functions and a timeout of 10 ticks. */ +UTEST_TC_EXPORT(testcase, "components.libc.cpp.thread_tc", utest_tc_init, utest_tc_cleanup, 10); \ No newline at end of file From 5983ccb50418913697f028dd5608377b1b9da89a Mon Sep 17 00:00:00 2001 From: lct1001 <2739960959@qq.com> Date: Thu, 13 Nov 2025 18:29:38 +0800 Subject: [PATCH 4/4] modify components/libc/cplusplus/os/cxx_Thread.cpp --- components/libc/cplusplus/os/cxx_Thread.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/components/libc/cplusplus/os/cxx_Thread.cpp b/components/libc/cplusplus/os/cxx_Thread.cpp index 7b068285224..b5ef2f5ffc2 100644 --- a/components/libc/cplusplus/os/cxx_Thread.cpp +++ b/components/libc/cplusplus/os/cxx_Thread.cpp @@ -19,10 +19,10 @@ using namespace rtthread; * @param name Thread name */ Thread::Thread(rt_uint32_t stack_size, - rt_uint8_t priority, + rt_uint8_t priority, rt_uint32_t tick, - const char *name) : - _entry(RT_NULL), _param(RT_NULL), started(false) + const char *name) + : _entry(RT_NULL), _param(RT_NULL), started(false) { rt_event_init(&_event, name, 0); @@ -46,10 +46,10 @@ Thread::Thread(rt_uint32_t stack_size, Thread::Thread(void (*entry)(void *p), void *p, rt_uint32_t stack_size, - rt_uint8_t priority, + rt_uint8_t priority, rt_uint32_t tick, - const char *name) : - _entry(entry), _param(p), started(false) + const char *name) + : _entry(entry), _param(p), started(false) { rt_event_init(&_event, name, 0); @@ -136,9 +136,9 @@ rt_err_t Thread::wait(int32_t millisec) } /** - * @brief the thread with a timeout. + * @brief Join the thread with a timeout. * @param millisec Timeout in milliseconds. - * @return Status code indicating the execution status. + * @return RT_EOK if the thread completed within the timeout, error code otherwise. */ rt_err_t Thread::join(int32_t millisec) { @@ -156,4 +156,4 @@ rt_err_t Thread::join(int32_t millisec) { return -RT_ENOSYS; } -} +} \ No newline at end of file