c - 主函数包含对 exit() 和 pthread_exit() 的调用。这两个调用在执行时的效果会有什么不同?

标签 c multithreading exit

我有一个线程函数定义如下: exit() 和 pthread_exit() 有什么区别?

result = pthread_create(&consumer_thread, NULL, consumer_routine, &queue);
if (0 != result) {
    fprintf(stderr, "Failed to create consumer thread: %s\n", strerror(result));
    exit(1);
}

result = pthread_join(consumer_thread, &thread_return);
if (0 != result) {
    fprintf(stderr, "Failed to join consumer thread: %s\n", strerror(result));
    pthread_exit(NULL);
}

最佳答案

exit 函数正常终止进程,刷新缓冲区,调用 atexit 处理程序,等等。 pthread_exit 函数终止调用线程,仅当它是进程中的最后一个线程时才终止进程。否则,进程中的其他线程可以继续运行。

关于c - 主函数包含对 exit() 和 pthread_exit() 的调用。这两个调用在执行时的效果会有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25925188/

相关文章:

Java - 在删除/获取上排序的线程安全集合

c# - 关闭所有子窗体时退出应用程序

java - 在后台运行一段时间后关闭应用程序

c - 错误 "Expected declaration in specifiers before ' 如果'"

将字符输入转换为十进制输出——不懂

ios - Alamofire 请求完成后如何在后台线程中解析 JSON?

java - 处理文件和数据库插入的最快方法 - Java 多线程

Java - 如何在不返回他所期望的情况下退出方法?

c++ - 将 uint8_t 与数字进行比较

卖香蕉的C程序