Merge pull request '我在master分支' (#3) from master into main

main
熊卓孜 7 months ago
commit 353e570ea1

@ -0,0 +1,218 @@
/*1212
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
static volatile int g_count = 0;
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
static int g_testAtforkCount = 0;
static int g_testAtforkPrepare = 0;
static int g_testAtforkParent = 0;
static int g_testAtforkChild = 0;
static const int SLEEP_TIME = 2;
static void Prepare()
{
int err;
ICUNIT_ASSERT_EQUAL_VOID(g_testAtforkCount, 1, g_testAtforkCount);
// 这是一个宏,用于检查条件是否满足。如果条件不满足,测试会停止并输出相应的信息。这里的作用是确保 g_testAtforkCount 等于 1。
err = pthread_mutex_lock(&g_lock);
// 这行代码试图获取互斥锁 g_lock如果获取成功g_testAtforkPrepare 会自增,表示该线程执行了准备工作。
ICUNIT_ASSERT_EQUAL_VOID(err, 0, err);
g_testAtforkPrepare++;
}
static void Parent()
{
int err;
ICUNIT_ASSERT_EQUAL_VOID(g_testAtforkCount, 1, g_testAtforkCount);
err = pthread_mutex_unlock(&g_lock);
// 这行代码释放之前在 Prepare 函数中锁定的互斥量 g_lock允许其他线程访问临界区。
ICUNIT_ASSERT_EQUAL_VOID(err, 0, err);
g_testAtforkParent++;
// 增加 g_testAtforkParent表示父进程执行完其相关操作。
}
static void child()
{
int err;
ICUNIT_ASSERT_EQUAL_VOID(g_testAtforkCount, 1, g_testAtforkCount);
err = pthread_mutex_unlock(&g_lock);
ICUNIT_ASSERT_EQUAL_VOID(err, 0, err);
g_testAtforkChild++;
// 增加 g_testAtforkChild表示子进程执行完其相关操作。
}
static void *ThreadProc(void *arg)
{
int err;
// 进入一个循环,直到 g_count 达到 5。
while (g_count < 5) {
// 锁住互斥量 g_lock保护 g_count 的更新,防止并发修改。
err = pthread_mutex_lock(&g_lock);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 增加 g_count 的值,表示执行了一次计数。
g_count++;
// 调用 SLEEP_AND_YIELD 宏函数,让当前线程休眠一段时间并让出 CPU允许其他线程执行。
SLEEP_AND_YIELD(SLEEP_TIME);
// 解锁互斥量 g_lock允许其他线程访问被保护的资源。
err = pthread_mutex_unlock(&g_lock);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 再次调用 SLEEP_AND_YIELD 宏函数,让当前线程休眠并让出 CPU。
SLEEP_AND_YIELD(SLEEP_TIME);
}
EXIT:
return NULL;
}
static void *PthreadAtforkTest(void *arg)
{
int err;
pid_t pid;
pthread_t tid;
int status = 0;
// 初始化共享变量 g_count 和测试相关的变量
g_count = 0;
g_testAtforkCount = 0;
g_testAtforkPrepare = 0;
g_testAtforkParent = 0;
g_testAtforkChild = 0;
// 创建一个新的线程
err = pthread_create(&tid, NULL, ThreadProc, NULL);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 注册 fork 前的处理函数
err = pthread_atfork(Prepare, Parent, child);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 更新测试计数器,标记已创建线程
g_testAtforkCount++;
// 休眠并让出 CPU 控制
SLEEP_AND_YIELD(SLEEP_TIME);
// 调用 fork() 创建子进程
pid = fork();
ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT); // 100000, PID 不会超过 100000
ICUNIT_GOTO_EQUAL(g_testAtforkPrepare, 1, g_testAtforkPrepare, EXIT);
// 子进程的处理逻辑
if (pid == 0) {
// 子进程中,检查 g_testAtforkChild 是否为 1确认子进程的状态
ICUNIT_GOTO_EQUAL(g_testAtforkChild, 1, g_testAtforkChild, EXIT);
// 子进程进入循环,直到 g_count 达到 5
while (g_count < 5) {
// 加锁,保护 g_count
err = pthread_mutex_lock(&g_lock);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 增加 g_count
g_count++;
// 休眠并让出 CPU 控制
SLEEP_AND_YIELD(SLEEP_TIME);
// 解锁
err = pthread_mutex_unlock(&g_lock);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 休眠并让出 CPU 控制
SLEEP_AND_YIELD(SLEEP_TIME);
}
// 子进程退出,设置退出状态为 15
exit(15); // 15, 设置退出状态
}
// 父进程中,检查 g_testAtforkParent 是否为 1确认父进程的状态
ICUNIT_GOTO_EQUAL(g_testAtforkParent, 1, g_testAtforkParent, EXIT_WAIT);
// 等待子线程结束
err = pthread_join(tid, NULL);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT_WAIT);
//
// 父进程等待子进程退出
err = waitpid(pid, &status, 0);
status = WEXITSTATUS(status); // 获取子进程的退出状态
ICUNIT_GOTO_EQUAL(err, pid, err, EXIT);
ICUNIT_GOTO_EQUAL(status, 15, status, EXIT); // 检查子进程的退出状态是否为 15
EXIT:
return NULL;
EXIT_WAIT:
// 父进程等待子进程退出
(void)waitpid(pid, &status, 0);
return NULL;
}
static int Testcase()
{
int ret;
pthread_t newPthread;
int curThreadPri, curThreadPolicy;
pthread_attr_t a = { 0 };
struct sched_param param = { 0 };
ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
curThreadPri = param.sched_priority;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri + 2; // 2, adjust the priority.
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, PthreadAtforkTest, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(newPthread, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return 0;
}
void ItTestPthreadAtfork001(void)
{
TEST_ADD_CASE("IT_PTHREAD_ATFORK_001", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}

@ -0,0 +1,211 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
#include <time.h>
// 引入系统函数nanosleep该函数用于使当前线程休眠指定的时间。
extern int nanosleep(const struct timespec *req, struct timespec *rem);
// 定义全局互斥锁 g_mux用于同步多线程操作。
static pthread_mutex_t g_mux = PTHREAD_MUTEX_INITIALIZER;
// 定义全局变量跟踪fork相关的操作计数和状态。
static volatile int g_testAtforkCount = 0; // 记录fork次数的计数器
static int g_testAtforkPrepare = 0; // 用于标识准备状态的计数器
static int g_testAtforkParent = 0; // 用于标识父进程状态的计数器
// 线程函数Doit用于在锁保护下进行nanosleep操作。
static void *Doit(void *arg)
{
int err;
struct timespec ts = { 2, 0 }; // 设置休眠时间为2秒第二个参数为纳秒0表示不需要额外的纳秒部分
// 检查g_testAtforkCount是否为1如果不是跳转到EXIT标签。
ICUNIT_GOTO_EQUAL(g_testAtforkCount, 1, g_testAtforkCount, EXIT);
// 锁住g_mux互斥锁保证对共享资源的访问是线程安全的。
err = pthread_mutex_lock(&g_mux);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 让当前线程休眠2秒。
(void)nanosleep(&ts, NULL);
// 解锁g_mux互斥锁。
err = pthread_mutex_unlock(&g_mux);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
EXIT:
// 函数结束返回NULL表示没有错误。
return NULL;
}
// 线程函数Doit1与Doit函数功能相同都是进行互斥锁保护下的nanosleep操作。
static void *Doit1(void *arg)
{
int err;
struct timespec ts = { 2, 0 }; // 设置休眠时间为2秒。
// 检查g_testAtforkCount是否为1如果不是跳转到EXIT标签。
ICUNIT_GOTO_EQUAL(g_testAtforkCount, 1, g_testAtforkCount, EXIT);
// 锁住g_mux互斥锁保证对共享资源的访问是线程安全的。
err = pthread_mutex_lock(&g_mux);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
// 让当前线程休眠2秒。
(void)nanosleep(&ts, NULL);
// 解锁g_mux互斥锁。
err = pthread_mutex_unlock(&g_mux);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT);
EXIT:
// 函数结束返回NULL表示没有错误。
return NULL;
}
// Prepare函数用于准备阶段释放锁并更新g_testAtforkPrepare计数器。
static void Prepare(void)
{
int err;
// 尝试解锁g_mux互斥锁若没有锁住则返回错误。
err = pthread_mutex_unlock(&g_mux);
ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); // 校验解锁是否成功,若不成功则会引发错误。
// 更新g_testAtforkPrepare计数器表示已准备好。
g_testAtforkPrepare++;
}
// Parent函数用于父进程阶段获取锁并更新g_testAtforkParent计数器。
static void Parent(void)
{
int err = pthread_mutex_lock(&g_mux); // 锁住g_mux互斥锁。
ICUNIT_ASSERT_EQUAL_VOID(err, 0, err); // 校验锁住是否成功,若不成功则会引发错误。
// 更新g_testAtforkParent计数器表示父进程已进入阶段。
g_testAtforkParent++;
}
// PthreadAtforkTest函数的作用是进行多线程与fork操作的测试验证pthread_atfork()的行为。
static void *PthreadAtforkTest(void *arg)
{
int err, ret;
int pid;
int status = 0;
struct timespec ts = { 1, 0 }; // 定义一个timespec结构指定nanosleep时休眠1秒。
pthread_t tid; // 定义一个线程ID变量用于存储创建的线程的ID。
// 初始化全局变量跟踪fork相关的操作计数和状态。
g_testAtforkCount = 0;
g_testAtforkPrepare = 0;
g_testAtforkParent = 0;
// 使用pthread_atfork注册处理函数分别为子进程准备函数Prepare和父进程函数Parent。
// NULL表示没有为子进程注册处理函数。
err = pthread_atfork(Prepare, Parent, NULL);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); // 如果pthread_atfork返回错误跳转到EXIT标签。
// 将g_testAtforkCount计数器加1表示准备开始执行fork操作。
g_testAtforkCount++;
// 创建一个新线程线程的执行函数是Doit。
err = pthread_create(&tid, NULL, Doit, NULL);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT); // 如果pthread_create失败跳转到EXIT标签。
// 让当前线程休眠1秒确保线程和主线程有足够的时间进入适当的执行阶段。
nanosleep(&ts, NULL);
// 执行fork系统调用创建一个新进程。
pid = fork();
ICUNIT_GOTO_EQUAL(g_testAtforkPrepare, 1, g_testAtforkPrepare, EXIT); // 确保在fork后Prepare函数已经被调用。
if (pid == 0) { // 子进程执行的代码。
// 调用Doit1函数子进程将在此执行休眠2秒。
Doit1(NULL);
exit(10); // 10表示子进程的退出状态。
}
// 检查pid值是否合法确保fork操作成功并且pid不会超过100000。
ICUNIT_GOTO_WITHIN_EQUAL(pid, 0, 100000, pid, EXIT_WAIT);
// 确保父进程调用了Parent函数更新了g_testAtforkParent计数器。
ICUNIT_GOTO_EQUAL(g_testAtforkParent, 1, g_testAtforkParent, EXIT_WAIT);
// 等待创建的线程结束。
err = pthread_join(tid, NULL);
ICUNIT_GOTO_EQUAL(err, 0, err, EXIT_WAIT); // 如果pthread_join失败跳转到EXIT_WAIT标签。
// 等待子进程结束,并获取其退出状态。
err = waitpid(pid, &status, 0);
status = WEXITSTATUS(status); // 获取子进程的退出状态。
ICUNIT_GOTO_EQUAL(err, pid, err, EXIT); // 检查waitpid返回值是否与pid一致。
ICUNIT_GOTO_EQUAL(status, 10, status, EXIT); // 确保子进程的退出状态是10。
EXIT:
return NULL; // 函数结束返回NULL。
EXIT_WAIT:
// 如果到达EXIT_WAIT标签表示需要等待子进程的结束防止僵尸进程。
(void)waitpid(pid, 0, 0);
return NULL;
}
static int Testcase(void)
{
int ret;
pthread_t newPthread;
int curThreadPri, curThreadPolicy;
pthread_attr_t a = { 0 };
struct sched_param param = { 0 };
ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
curThreadPri = param.sched_priority;
ret = pthread_attr_init(&a);
pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
param.sched_priority = curThreadPri + 2; // 2, adjust the priority.
pthread_attr_setschedparam(&a, &param);
ret = pthread_create(&newPthread, &a, PthreadAtforkTest, 0);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ret = pthread_join(newPthread, NULL);
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
return 0;
}
void ItTestPthreadAtfork002(void)
{
TEST_ADD_CASE("IT_PTHREAD_ATFORK_002", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
}

@ -0,0 +1,128 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "it_pthread_test.h"
static pthread_mutex_t g_pthreadMuxTest1;
static pthread_cond_t g_pthdCondTest1;
static void *PthreadF01(void *t)
{
int rc;
rc = pthread_mutex_lock(&g_pthreadMuxTest1); // 尝试获取互斥锁 g_pthreadMuxTest1
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 如果获取锁失败,跳转到 EXIT确保返回值为0表示成功获取锁
g_testCount++; // 增加 g_testCount表示线程正在执行
LosTaskDelay(100); // 延时 100模拟线程运行期间的某些操作例如 I/O 操作或计算等)
ICUNIT_GOTO_EQUAL(g_testCount, 2, g_testCount, EXIT); // 确保此时 g_testCount 等于 2表示线程执行过程中计数值的正确性
g_testCount++; // 增加 g_testCount为后续操作做准备
rc = pthread_cond_wait(&g_pthdCondTest1, &g_pthreadMuxTest1); // 释放互斥锁,等待条件变量 g_pthdCondTest1 被唤醒
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 确保等待条件变量的调用没有出错返回值为0表示成功等待
ICUNIT_GOTO_EQUAL(g_testCount, 5, g_testCount, EXIT); // 确保此时 g_testCount 的值为 5表示线程同步操作正确
rc = pthread_mutex_unlock(&g_pthreadMuxTest1); // 释放互斥锁,允许其他线程操作共享资源
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 确保释放锁没有出错返回值为0表示成功释放锁
EXIT:
return NULL; // 线程执行完毕,返回 NULL
}
static void *PthreadF02(void *t)
{
int i;
int rc;
ICUNIT_GOTO_EQUAL(g_testCount, 1, g_testCount, EXIT); // 确保此时 g_testCount 为 1表示线程同步的正确性
g_testCount++; // 增加 g_testCount为后续操作做准备
rc = pthread_mutex_lock(&g_pthreadMuxTest1); // 获取互斥锁 g_pthreadMuxTest1
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 确保获取锁没有出错
ICUNIT_GOTO_EQUAL(g_testCount, 3, g_testCount, EXIT); // 确保此时 g_testCount 等于 3表示同步操作执行的正确性
g_testCount++; // 增加 g_testCount表示线程操作继续
rc = pthread_cond_signal(&g_pthdCondTest1); // 唤醒等待条件变量 g_pthdCondTest1 的线程
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 确保条件变量唤醒操作成功返回值应为0
ICUNIT_GOTO_EQUAL(g_testCount, 4, g_testCount, EXIT); // 确保此时 g_testCount 的值为 4表示线程同步的正确性
g_testCount++; // 增加 g_testCount为后续操作做准备
rc = pthread_mutex_unlock(&g_pthreadMuxTest1); // 释放互斥锁,允许其他线程访问共享资源
ICUNIT_GOTO_EQUAL(rc, 0, rc, EXIT); // 确保释放锁操作没有出错
LosTaskDelay(2); // 延时 2控制线程执行的时机确保测试结果稳定
EXIT:
pthread_exit(NULL); // 线程执行完毕,退出
}
static unsigned int TestCase(void)
{
int i; // 用于循环控制
long t1 = 1; // 线程1的参数 t1
long t2 = 2; // 线程2的参数 t2
int rc; // 用于存储函数调用的返回值,检测调用是否成功
pthread_t threads[3]; // 定义一个数组包含3个线程尽管这里只创建了两个线程0和1此数组为初始化时的预留空间
pthread_attr_t attr; // 线程属性变量,用于设置线程的属性(但在此代码中未设置具体属性)
const int loopNum = 2; // 设置线程等待并回收的次数
g_testCount = 0; // 初始化全局变量 g_testCount为0表示线程同步的初始状态
pthread_mutex_init(&g_pthreadMuxTest1, NULL); // 初始化互斥锁 g_pthreadMuxTest1
pthread_cond_init(&g_pthdCondTest1, NULL); // 初始化条件变量 g_pthdCondTest1
rc = pthread_create(&threads[0], NULL, PthreadF01, (void *)t1); // 创建线程0执行 PthreadF01并传递 t1 作为参数
ICUNIT_ASSERT_EQUAL(rc, 0, rc); // 确保线程创建成功返回值应为0
rc = pthread_create(&threads[1], NULL, PthreadF02, (void *)t2); // 创建线程1执行 PthreadF02并传递 t2 作为参数
ICUNIT_ASSERT_EQUAL(rc, 0, rc); // 确保线程创建成功返回值应为0
// 主线程等待两个子线程的执行结束
for (i = 0; i < loopNum; i++) {
rc = pthread_join(threads[i], NULL); // 等待线程 i 执行完毕并回收资源
ICUNIT_ASSERT_EQUAL(rc, 0, rc); // 确保线程加入操作成功返回值应为0
}
rc = pthread_attr_destroy(&attr); // 销毁线程属性对象,释放资源
ICUNIT_ASSERT_EQUAL(rc, 0, rc); // 确保销毁操作成功
rc = pthread_mutex_destroy(&g_pthreadMuxTest1); // 销毁互斥锁,释放资源
ICUNIT_ASSERT_EQUAL(rc, 0, rc); // 确保销毁操作成功
rc = pthread_cond_destroy(&g_pthdCondTest1); // 销毁条件变量,释放资源
ICUNIT_ASSERT_EQUAL(rc, 0, rc); // 确保销毁操作成功
return 0; // 返回0表示测试用例执行完毕
}
void ItTestPthreadCond001(void)
{
TEST_ADD_CASE("IT_POSIX_PTHREAD_COND_001", TestCase, TEST_POSIX, TEST_PTHREAD, TEST_LEVEL2, TEST_FUNCTION);
}
Loading…
Cancel
Save