所以,作为前言,我是C的新手,所以请原谅我的课程有任何可怕的错误。
我正在尝试编写一个程序,该程序将作为参数传递给程序或包含在传递路径的文件中。
它将数字存储到数组中,并存储数组的第一个元素中有多少个数字。它最多只能存储100个数字。
然后创建一个pthread并将指向数组的指针传递给线程。
然后假设线程总结数字并将总和返回主函数。
我遇到了以下问题:
1。它并不总是发生,但有时我会在代码行之前得到一个分段错误:
printf("Begining to create the thread");
2。我尝试归还这笔款项是行不通的,经过数小时的网上研究,我无法弄清楚原因。
3。编译程序时,出现以下错误:
gcc -g -o assn3 assn3.c -pthread
assn3.c: In function ‘AddUpNumbers’:
assn3.c:34:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
return (void*)total;
^
assn3.c: In function ‘main’:
assn3.c:96:3: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast [enabled by default]
pthread_join(&functionThread, &total);
^
In file included from assn3.c:12:0:
/usr/include/pthread.h:261:12: note: expected ‘pthread_t’ but argument is of type ‘pthread_t *’
extern int pthread_join (pthread_t __th, void **__thread_return);
^
assn3.c:97:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
printf("The total returned by the thread is %d", total);
^
这是我的代码:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NO_ARGS 1
#define ONLY_SINGLE_ARG 2
#define PATH_TO_READ_FROM 1
#define MAX_NUMBERS 101
#define MAX_CHAR_INPUT 255
#define COUNT_LOCATION 0
void* AddUpNumbers(void* arrayPointer) {
printf("Created the pthread!");
int* numbersArray = (int*)arrayPointer;
int count = numbersArray[COUNT_LOCATION];
int total = 0;
int i = 1;
while (i < count) {
total = total + numbersArray[i];
}
printf("The total to be returned is %d", total);
return (void*)total;
}
int main(int argc, char* argv[]) {
FILE * numbersFile = NULL;
int count = 0;
int numberArray[MAX_NUMBERS];
//Initialize the Array
int i = 0;
while (i < MAX_NUMBERS) {
numberArray[i] = 0;
i = i + 1;
}
if (argc == NO_ARGS) {
printf("Usage: # or file path, #, #, ..., #\n");
} else if (argc == ONLY_SINGLE_ARG) {
numbersFile = fopen(argv[PATH_TO_READ_FROM], "r");
if (numbersFile != NULL) {
char buff[MAX_CHAR_INPUT];
i = 1;
count = 0;
while (i < MAX_NUMBERS) {
if (fscanf(numbersFile, "%s", buff) != EOF) {
numberArray[i] = atoi(buff);
printf("%d\n", numberArray[i]);
i = i + 1;
count = count + 1;
} else {
break;
}
}
numberArray[COUNT_LOCATION] = count;
printf("Count Total: %d\n", numberArray[COUNT_LOCATION]);
} else {
printf("Error: Could not open file!\n");
return -1;
}
} else if (argc < MAX_NUMBERS + 1) {
i = 1;
count = 0;
while (i < argc) {
numberArray[i] = atoi(argv[i]);
printf("%d\n", numberArray[i]);
i = i + 1;
count = count + 1;
}
printf("See if error happens after this");
numberArray[COUNT_LOCATION] = count;
printf("Count Total: %d\n", numberArray[COUNT_LOCATION]);
} else {
printf("Too many numbers! This program can only add up to: %d numbers.\n", MAX_NUMBERS);
return -1;
}
printf("Begining to create the thread");
pthread_t functionThread;
int creationSuccess = 0;
void* total;
creationSuccess = pthread_create(&functionThread, NULL, AddUpNumbers, (void*)numberArray);
if (creationSuccess == 0) {
pthread_join(&functionThread, total);
printf("The total returned by the thread is %d", *((int)total));
} else {
printf("Something went wrong.\n");
}
if (numbersFile != NULL) {
fclose(numbersFile);
}
return 0;
}
我的Makefile看起来像这样:
assn3: assn3.c
gcc -g -o assn3 assn3.c -pthread
答案 0 :(得分:1)
您应该非常警惕编译器警告。要么清理它们,要么很清楚为什么它们没问题。请特别注意有关数据类型不匹配的警告。
在这种情况下,此警告可能解释了主要问题:
In file included from assn3.c:12:0: /usr/include/pthread.h:261:12: note: expected ‘pthread_t’ but argument is of type ‘pthread_t *’ extern int pthread_join (pthread_t __th, void **__thread_return); ^
您正在(创建并)传递指向pthread_t
对象的指针作为pthread_join()
的第一个参数,但与pthread_create()
不同,pthread_join()
期望您传递{ {1}}本身,而不是指向它的指针。随之而来的是各种各样的破坏(技术上,&#34;未定义的行为&#34;)。
UPDATE :此外,您传递给pthread_t
的第二个参数是指向pthread_join()
的未初始化指针。如果void
尝试写任何指向的地方,那么谁知道会发生什么(再次定义未定义的行为)。您应该将有效指针传递给要写入结果的位置。在这种情况下,那将是pthread_create()
。
答案 1 :(得分:0)
pthread_create
的语法是:
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, *(*start_routine) (void *), void *arg);
pthread_t - 是线程的id。因此,如果您有很多线程,请创建变量pthread_t id
或值数组,例如pthread_t id[THREAD_NUM];
然后你的func会看起来像:
pthread_create(&id[i], NULL, &functionThread, (void*)numberArray);
With pthread_join(&functionThread, total);
同样的事情。
int pthread_join(pthread_t thread, void **value_ptr);
所以你加入必须看起来像:
pthread_join(&id[i], total);