MST

星途 面试题库

面试题:C语言多线程错误处理之资源清理

在Linux C语言多线程编程中,假设某个线程负责打开文件并进行读写操作,当该线程出现错误时,如何确保文件资源能够被正确关闭以避免资源泄漏?请给出具体代码示例。
34.5万 热度难度
编程语言C

知识考点

AI 面试

面试题答案

一键面试

在Linux C语言多线程编程中,可以通过使用pthread_cleanup_pushpthread_cleanup_pop函数来确保文件资源在出现错误时能被正确关闭。以下是具体代码示例:

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

// 线程函数声明
void* thread_function(void* arg);

// 清理函数,用于关闭文件
void cleanup_handler(void* arg) {
    int fd = *((int*)arg);
    close(fd);
    printf("File closed in cleanup handler.\n");
}

int main() {
    pthread_t tid;
    int ret;

    // 创建线程
    ret = pthread_create(&tid, NULL, thread_function, NULL);
    if (ret != 0) {
        fprintf(stderr, "Error creating thread: %d\n", ret);
        return 1;
    }

    // 等待线程结束
    ret = pthread_join(tid, NULL);
    if (ret != 0) {
        fprintf(stderr, "Error joining thread: %d\n", ret);
        return 1;
    }

    return 0;
}

void* thread_function(void* arg) {
    int fd;
    // 打开文件
    fd = open("test.txt", O_RDWR | O_CREAT, 0666);
    if (fd == -1) {
        perror("open");
        return NULL;
    }

    // 注册清理函数
    pthread_cleanup_push(cleanup_handler, &fd);

    // 模拟一些可能出错的操作
    if (1) { // 这里简单假设出错情况
        printf("Simulated error occurred.\n");
        // 出错时,清理函数会被调用关闭文件
        pthread_exit(NULL);
    }

    // 取消注册清理函数(如果没有出错)
    pthread_cleanup_pop(0);

    // 正常的文件读写操作
    // 这里省略具体的读写代码

    close(fd);
    printf("File closed normally.\n");
    pthread_exit(NULL);
}

在上述代码中:

  1. cleanup_handler函数用于关闭文件描述符。
  2. pthread_cleanup_push用于注册清理函数,当线程通过pthread_exit、响应取消请求或调用pthread_cleanup_pop(1)时,注册的清理函数会被调用。
  3. pthread_cleanup_pop(0)用于取消注册清理函数,如果传递参数为1,则会调用清理函数并取消注册。在正常情况下,文件会在pthread_cleanup_pop(0)之后被正常关闭;如果在pthread_cleanup_push之后出现错误,清理函数会被调用以关闭文件,避免资源泄漏。