Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用指针将Pthread返回的多个数组存储在另一个数组中_C_Multithreading_Pointers_Concurrency_Pthreads - Fatal编程技术网

C 如何使用指针将Pthread返回的多个数组存储在另一个数组中

C 如何使用指针将Pthread返回的多个数组存储在另一个数组中,c,multithreading,pointers,concurrency,pthreads,C,Multithreading,Pointers,Concurrency,Pthreads,我正在处理一个需要同时运行多个线程的项目。每个线程运行一个函数,该函数返回指向int数组的指针(强制转换为空指针)。例如,在线程运行的函数中,我想要的值存储为: void *func(void *args) { int vals[3] = {0, 0, 0}, x = y = z = 0; int *ptr = vals; while(condition) { . . . ptr[0] += x;

我正在处理一个需要同时运行多个线程的项目。每个线程运行一个函数,该函数返回指向int数组的指针(强制转换为空指针)。例如,在线程运行的函数中,我想要的值存储为:

void *func(void *args) {
    int vals[3] = {0, 0, 0}, x = y = z = 0;
    int *ptr = vals;

    while(condition) {
        .
        .
        .
        ptr[0] += x;
        ptr[1] += y;
        ptr[2] += z;
    }
    return (void *) ptr;
}
此函数结束时,ptr[0]、ptr[1]和ptr[2]保持所需的值。这段代码只是让您了解发生了什么,我的实际实现没有任何问题

我需要使用pthread_join()从每个线程获取各种值。每个线程处理一个文件,因此#of files==#of threads==#上述函数运行的次数

我需要从每个线程中获取ptr[0]、ptr[1]和ptr[2]的值,并在返回到main后将它们相加。也就是说,如果有三个线程,那么我需要将线程1中的ptr[0]添加到线程2中的ptr[0]和线程3中的ptr[0]。ptr[1]和ptr[2]也是如此。然后我需要在最后打印main中的三个总值。到目前为止,我就是这样做的,我得到了要编译的代码,但这些值都是垃圾

int main(int argc, char *argv[]) {
    int NUM_THREADS = argc - 1;
    int total1 = total2 = total3 = 0;
    pthread_t *tids; /* Dynamically allocated array of pthreads */
    void **vals; /* Stores the return values from each thread, this could be incorrect */

    /*
    ** Some code where I allocate the arrays etc
    */

    for (i= 0; i < NUM_THREADS; i++)
      pthread_create(&tids[i], NULL, func, NULL);

    for (i= 0; i < NUM_THREADS; i++)
      pthread_join(tids[i], &(vals[i])); /* Again, this could be wrong */

    for (i= 0; i < NUM_THREADS; i++) {
      total1 += ((int*) vals[i])[0]; /* These statements very well could also be wrong */
      total2 += ((int*) vals[i])[1];
      total3 += ((int*) vals[i])[2];
    }

    /* Print totals */

    return 0;
}
intmain(intargc,char*argv[]){
int NUM_THREADS=argc-1;
int total1=total2=total3=0;
pthread_t*tids;/*动态分配的pthread数组*/
void**vals;/*存储每个线程的返回值,这可能不正确*/
/*
**一些我分配数组的代码等等
*/
对于(i=0;i
我知道每个线程中的值在func结束时都是正确的,但我不知道如何在main中正确地存储、处理和打印它们

另外,我使用的是C90,不能使用任何其他版本的C中的任何功能,我必须使用pthread_join()存储这些值

每个线程运行一个函数,该函数返回指向int数组的指针(强制转换为空指针)。比如说

void *func(void *args) {
    int vals[3] = {0, 0, 0}, x = y = z = 0;
    int *ptr = vals;
...
    return (void *) ptr;
}
您应该首先修复函数以返回有意义的内容。正如当前编写的那样,函数返回指向自动数组
VAL
的指针,该指针在函数返回后不再存在

使用返回的指针将产生未定义的行为

这段代码只是让您了解发生了什么,我的实际实现没有任何问题

这有点难以置信

pthread_join(tids[i],&(vals[i]);/*同样,这可能是错误的*/

这是错误的:
vals
此时未初始化,因此您正在随机内存中进行写入

我不知道如何在main中正确地存储、处理和打印它们

以下是如何:

  void *vals[NUM_THREADS];
  ..
  for (i= 0; i < NUM_THREADS; i++) {
    pthread_join(tids[i], &vals[i]);
  }
  for (i= 0; i < NUM_THREADS; i++) {
    int *p = (int *)vals[i];
    total1 += p[0];
    total2 += p[1];
      ... etc.
  }
void*vals[NUM_线程];
..
对于(i=0;i