1

学习 pthreads,但我在创建它们时遇到了麻烦。这是我的输出和 gdb 信息:

在 main() 中,创建线程 1
错误:线程 1 的 pthread_create() 返回代码为 22
在 main() 中,创建线程 2
错误:线程 2 的 pthread_create() 返回代码为 22
在 main() 中,创建线程 3
错误:线程 3 的 pthread_create() 返回代码为 22
在 main() 中,创建线程 4
错误:线程 4 的 pthread_create() 返回代码为 22
在 main() 中,创建线程 5
错误:线程 5 的 pthread_create() 的返回码为 22

程序收到信号 SIGSEGV,分段错误。0xb7fb4d8a 在
pthread_join (threadid=76038327, thread_return=0x0)
    在 pthread_join.c:46 46 pthread_join.c: 没有这样的文件或目录。

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>

#define SBUFSIZE 1025

char errorstr[SBUFSIZE];
FILE* inputfp[5];

void* f(void* inpFile) {
    fprintf(stderr, "%s\n", (char*)inpFile);
    return NULL;
}

int main (int argc, char* argv[]) {

    int i;

    /* Thread Variables */
    pthread_attr_t attr;
    pthread_t *th[argc-2]; //one thread for each input file

    /* allocate memory for the threads */
    for (i = 0; i < (argc-2); i++) {
        th[i] = (pthread_t *) malloc(sizeof(pthread_t)); 
        inputfp[i] = fopen(argv[i], "r");
        if (!inputfp[i]) {
            sprintf(errorstr, "Error Opening Input File: %s", argv[i]);
            perror(errorstr);
        }
    }

    /* Create one thread for each input file */
    for (i = 1; i < (argc - 1); i++) {
        fprintf (stderr, "In main(), creating thread %1d\n", i);
        int rc = pthread_create (th[i], &attr, f, inputfp[i-1]);
        if (rc) {
            printf("ERROR: return code from pthread_create() is %d for thread %d\n",
                rc, i);
        }
    }

    /* wait for the threads to finish */
    for (i = 1; i < (argc - 1); i++) {
        pthread_join(*th[i], 0);
    }

    return EXIT_SUCCESS;
}

我错过了一些东西,但我不知道是什么。任何人都可以帮忙吗?谢谢!

编辑:这是我根据 Joachim Pileborg 的建议更改代码的方式。我仍然收到从 pthread_create() 返回的错误 22,但 pthread_join 上的 SIGSEGV 错误不再发生。

任何人都对我如何让 pthread_create() 返回 0 (表示线程创建成功)有任何建议?再次感谢!

int main (int argc, char* argv[]) {

    int i;

    /* Thread Variables */
    pthread_attr_t attr;
    pthread_t *th[argc-2]; //one thread for each input file

    /* allocate memory for the threads */
    for (i = 0; i < (argc-2); i++) {
        th[i] = (pthread_t *) malloc(sizeof(pthread_t)); 
        printf("%s\n", argv[i+1]);
        inputfp[i] = fopen(argv[i+1], "r");
        if (!inputfp[i]) {
            sprintf(errorstr, "Error Opening Input File: %s", argv[i]);
            perror(errorstr);
        }
    }

    /* Create one thread for each input file */
    for (i = 0; i < (argc - 2); i++) {
        fprintf (stderr, "In main(), creating thread %1d\n", i);
        int rc = pthread_create (th[i], &attr, f, inputfp[i]);
        if (rc) {
            printf("ERROR: return code from pthread_create() is %d for thread %d\n",
                rc, i);
        }
    }

    /* wait for the threads to finish */
    for (i = 0; i < (argc - 2); i++) {
        pthread_join(*th[i], 0);
    }

    return EXIT_SUCCESS;
}
4

1 回答 1

1

您有一个循环,从零循环到argc - 3,并使用正确的索引(从零到“数组大小减一”。

然后你有两个循环,你从一个循环到argc - 2,并使用从一到“数组大小”的索引。

您应该在所有三个地方使用与第一个相同的循环。

于 2013-02-23T05:13:42.217 回答