pthread_create 启动例程未执行

pthread_create start routine not executing

我正在尝试为学校的 C 作业创建一个火车站模拟。这是一个理解线程编程的练习。这是我当前的代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <readline/readline.h>

void *train_function(void *args) {
    printf("Train created!\n");
    pthread_exit(0);
}

int main() {
    FILE *ptr_file;
    char buff[10];
    int ch;
    int train_count = 0;

    ptr_file = fopen("./trains.txt", "r");

    if (!ptr_file)
        return 0;

    /* Count number of trains */
    while(!feof(ptr_file))
    {
        ch = fgetc(ptr_file);
        if(ch == '\n')
        {
            train_count++;
        }
    }
    pthread_t trains[train_count];

    /* Create train for each line of file */
    int train_index = 0;
    while (fgets(buff,10, ptr_file)!=NULL) {
        pthread_create(&trains[train_index], NULL, train_function, NULL);
        train_index++;
    }
    fclose(ptr_file);

    /* Wait for all trains to leave the station */
    for (int x = 0; x < train_count; x++) {
        pthread_join(trains[x], NULL);
    }
    exit(0);
}

该代码从文件中读取有关火车的信息 trains.txt 并为文件的每一行(每列火车)创建一个新线程。

e:10,6
W:5,7
E:3,10

我希望输出是 "Train Created!" 三倍。编译代码会产生分段错误。我错过了什么?

 while (fgets(buff,10, ptr_file)!=NULL) {
        pthread_create(&trains[train_index], NULL, train_function, NULL);
        train_index++;
    }

在这里,您正在再次读取文件,但是文件指针已经位于文件末尾,因为您在此 while 循环之前已经读取了整个文件。因此,您根本没有创建任何线程,而是稍后尝试使用不存在的线程 join (pthread_join 调用)。这是未定义的行为。您只需要使用 train_count 并在 for 循环中创建线程:

 for (int x = 0; x < train_count; x++) {
    pthread_create(&trains[x], NULL, train_function, NULL);
 }

另外,注意文件读取部分有问题:

while(!feof(ptr_file)) {...}

Why is “while ( !feof (file) )” always wrong? 用于修复它。

代码有几个小问题。

以下代码删除了已发布代码中未使用的语句。

不要将 feof() 用作循环控件。由于多种原因,它无法作为循环控制。

代码中添加了必要的错误检查。

注意将正确的错误消息正确输出到 stderr 通过使用 perror() 函数

注意通过使用 exit() 函数退出的正确错误

注意 main 的正确退出,最后,通过使用 return() 函数

以下代码已经过测试并且可以正常工作。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
//#include <readline/readline.h>

void *train_function(void *args __attribute__((unused)) )
{
    printf("Train created!\n");
    pthread_exit(0);
}

int main()
{
    FILE *ptr_file;
    int ch;
    int train_count = 0;

    ptr_file = fopen("trains.txt", "r");

    if (!ptr_file)
    {
        perror( "fopen for trains.txt failed" );
        exit( EXIT_FAILURE);
    }

    // implied else, fopen successful

    /* Count number of trains */
    while( (ch = fgetc(ptr_file) ) != EOF)
    {
        if(ch == '\n')
        {
            train_count++;
        }
    }
    pthread_t trains[train_count];

    /* Create train for each line of file */
    int train_index = 0;
    for ( train_index = 0; train_index < train_count; train_index++ )
    {
        if( pthread_create(&trains[train_index], NULL, train_function, NULL) )
        { // then pthread_create failed
            perror( "pthread_create failed" );
            exit( EXIT_FAILURE ); // this will cause all threads to also exit
        }

        // implied else, pthread_create successful
    }

    fclose(ptr_file);

    /* Wait for all trains to leave the station */
    for (int x = 0; x < train_count; x++)
    {
        pthread_join(trains[x], NULL);
    }
    return(0);
} // end function: main