可以使用pthread_join函数来阻塞线程。pthread_join函数会等待指定的线程结束后才会返回。具体使用方法如下:

  1. 在主线程中调用pthread_create函数创建新线程,并保存线程的ID。

  2. 调用pthread_join函数,在主线程中等待新线程结束。

  3. 在新线程中,使用pthread_exit函数退出线程。

示例代码:

#include <pthread.h>
#include <stdio.h>

void *thread_func(void *arg) {
    printf("New thread started.\n");
    pthread_exit(NULL);
}

int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, thread_func, NULL);
    printf("Main thread waiting for new thread to exit.\n");
    pthread_join(thread, NULL);
    printf("New thread exited.\n");
    return 0;
}

在上面的示例代码中,主线程创建了一个新线程,并使用pthread_join函数等待新线程结束。在新线程中,使用pthread_exit函数退出线程。当新线程退出时,pthread_join函数返回,主线程继续执行

安卓c语言 pthread_create出来的线程如何阻塞?

原文地址: https://www.cveoy.top/t/topic/cRm0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录