gpt4 book ai didi

c - 难以创建 Pthread(错误 22)

转载 作者:太空宇宙 更新时间:2023-11-04 03:57:28 25 4
gpt4 key购买 nike

了解 pthreads,但我无法创建它们。这是我的输出和 gdb 信息:

In main(), creating thread 1 ERROR: return code from pthread_create() is 22 for thread 1In main(), creating thread 2ERROR: return code from pthread_create() is 22 for thread 2In main(), creating thread 3ERROR: return code from pthread_create() is 22 for thread 3In main(), creating thread 4ERROR: return code from pthread_create() is 22 for thread 4In main(), creating thread 5ERROR: return code from pthread_create() is 22 for thread 5Program received signal SIGSEGV, Segmentation fault. 0xb7fb4d8a inpthread_join (threadid=76038327, thread_return=0x0)    at pthread_join.c:46 46 pthread_join.c: No such file or directory.

And here is my code:

#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;
}

最佳答案

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

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

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

关于c - 难以创建 Pthread(错误 22),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15037234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com