vs2019 cpp20 规范的线程头文件 <thread> 注释( 4 ):函 thrd_detach (),thrd_equal (),thrd_current (),thrd_yield ()

(16) 函数 thrd_detach ()。因为至少线程控制块 TCB 要回收。类似于进程控制块 PCB 被父进程回收, 父进程要调用 wait_pid():

在这里插入图片描述

(17) 函数 thrd_equal ()

在这里插入图片描述

++ 给出举例:

#include <stdio.h>
#include <stdlib.h>
#include <threads.h>

void* thread_function(void* arg) {
    // 线程函数体
    return NULL;
}

int main() {
    thrd_t thread1, thread2;
    int result1, result2;

    // 创建两个线程
    result1 = thrd_create(&thread1, thread_function, NULL);
    if (result1 != thrd_success) {
        fprintf(stderr, "Failed to create thread 1\n");
        return EXIT_FAILURE;
    }

    result2 = thrd_create(&thread2, thread_function, NULL);
    if (result2 != thrd_success) {
        fprintf(stderr, "Failed to create thread 2\n");
        return EXIT_FAILURE;
    }

    // 比较线程标识符
    if (thrd_equal(&thread1, &thread2)) {
        printf("Thread 1 and Thread 2 are the same thread.\n");
    } else {
        printf("Thread 1 and Thread 2 are different threads.\n");
    }

    // 等待线程完成
    thrd_join(thread1, NULL);
    thrd_join(thread2, NULL);

    return EXIT_SUCCESS;
}

(18) 函数 thrd_current() :

在这里插入图片描述

(19)函数 thrd_yield()

在这里插入图片描述

++ 给出 举例:

#include <threads.h>
#include <stdio.h>
#include <stdlib.h>

// 线程函数
void* thread_function(void* arg) {
    for (int i = 0; i < 10; ++i) {
        printf("Thread %ld running... %d\n", (long)arg, i);
        // 在每次循环中调用 thrd_yield()
        thrd_yield();
    }
    return NULL;
}

int main() {
    thrd_t thread1, thread2;

    // 创建两个线程
    thrd_create(&thread1, thread_function, (void*)1L);
    thrd_create(&thread2, thread_function, (void*)2L);

    // 等待两个线程完成
    thrd_join(thread1, NULL);
    thrd_join(thread2, NULL);

    return 0;
}

但是该源代码 在 ubantu 与 vs2019 里的 c17 标准下都找不到系统函数。无法验证。

(20)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值