与 pthread_create 不兼容的指针类型 (C)

Incompatible pointer type with pthread_create (C)

我正在从事一个个人项目,该项目使用多线程拆分数组、搜索目标以及 return 匹配数。我的早期代码有一个错误和一些错误。

错误...

main.c:117:10: warning: passing argument 1 of 'pthread_create' from incompatible pointer type [enabled by default]

In file included from main.c:5:0: /usr/include/pthread.h:225:12: note: expected 'pthread_t * restrict' but argument is of type 'pthread_t **'

我是 POSIX 的新手,不知道这里出了什么问题。

错误...

我的大循环应该循环只要 index < totalElems 只循环一次。它进入小循环并退出两者而不是循环更多。我不确定这是为什么。

头文件...

#ifndef COUNT_ARRAY_H
#define COUNT_ARRAY_H


// structure declarations
typedef struct
{
 int     threadNum;
 int     *array;
 int     first;
 int     last;
 int     target;
 int     numFound;

} ThreadInfo;


// function prototypes
void*   ThreadFunc(void  *vptr);


#endif  // COUNT_ARRAY_H

。 .

Main.c 文件....

#include    <pthread.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    "count_array.h"

int     main(void)
{
 auto    int                 numSegs;
 auto    int                 numSegElems;
 auto    int                 maxRand;
 auto    int                 target;
 auto    int                 totalElems;
 auto    int                 totalFound = 0;
 auto    ThreadInfo          *infoPtr;
 auto    pthread_t           *threadHandles;
 auto    int                 index = 0;
 auto    int                 first;
 auto    int                 last;
 auto    int                 threadNum = 0;

//get primary info from user...
printf(" Please enter the total number of elements? ");
scanf("%d", &totalElems);

printf(" Please enter the maximum random value: ");
scanf("%d", &maxRand);

printf(" Please enter the number of segments (1 to 15857): ");
scanf("%d", &numSegs);

 if(numSegs > 15857)
   {
    puts(" Too many segments for machine!");
    exit(EXIT_FAILURE);
   }

numSegElems = totalElems/numSegs;

// configure the array to work with
// declare array here...

auto int myArray[totalElems];

//and fill array here
for(; index < totalElems; index++)
   {
    // % rand() and maxRand to get good range and
    //not go beyond users max number
    myArray[index] = (rand() % maxRand);

    //test printf...ignore if still here at 5/18/17 or later
     printf(" %d \n", myArray[index]);
   }

// get the target value to look for
 printf(" Please enter the target value: ");
 scanf("%d",&target);

// display initial information
printf("*** Begin search: target = %d, # elements = %d, # segments = %d, "
                                            "# segment elements = %d\n"
                                                            , target
                                                            , totalElems
                                                            , numSegs
                                                            , numSegElems);


// initialize the array first/last indexes into the integer array
// >>>50 elems total/5 = 10 threads total and 5 elems in each thread<<<
  for(index = 0; index < totalElems; index++)
     {

      first = myArray[0];

      if(index == numSegElems)
        {

         puts(" in if ");
         last = myArray[index];

         printf(" %d \n", index);


// allocate an array to store the thread handles

         auto int arraySeg[numSegElems];

 // loop and create threads (# of segments)

    // allocate a thread info structure from the heap
    //using malloc

         infoPtr = malloc(sizeof(ThreadInfo));
         if(NULL == infoPtr)
           {
            fprintf(stderr, "Unable to allocate ThreadInfo struct for "
                           "thread #%d\n", threadNum);

            continue;
           }
    // store the information in the allocated structure
         infoPtr->target = target;
         infoPtr->threadNum = threadNum;
         infoPtr->first = first;
         infoPtr->last = last;
         infoPtr->array = arraySeg;

    // create the secondary thread, passing the thread info
       if(pthread_create(&threadHandles, NULL, ThreadFunc, &infoPtr))
         {
          fprintf(stderr, "Error: failed to create thread #%d\n", 
          threadNum);
          continue;
         }

    // update the first/last array indexes for the next thread
    //set to zero again??

      }//end small loop to make individual threads

    //increment thread #
       ++threadNum;

    }//end big loop
      // loop and join the threads to fetch each thread's results

    // join with the next thread

        // get the total number of matches from the thread's infoPtr
        // and display a message

        // release the infoPtr structure back to the heap


// display the final results

// release heap memory


   return 0;

}  // end of "main"

我以前做过这样的小项目,但我没看出这里有什么问题。我需要传递 infoPtr 并使用 ThreadFunc 中的成员。我完全像我的其他程序一样这样做,但它不起作用。我已经尝试搜索网站和 google,但也许我无法弄明白,因为它太具体了?此外,删除 & 也无济于事。如有任何帮助,我们将不胜感激!

I'm not seeing what's wrong here.

这里有许多 处错误。

让我们考虑一个非常简化的测试用例:

pthread_t *threadHandles;
ThreadInfo *info;

for(index = 0; index < totalElems; index++) {
  info = malloc(sizeof(ThreadInfo));
  pthread_create(&threadHandles, NULL, ThreadFunc, &info);
}
return 0;

问题:

  • 您创建了 N 个线程,然后 return 无需等待其中任何一个。一旦 main returns,所有 线程随之蒸发(整个程序终止)。
  • 在循环的每次迭代中,pthread_create 将一个 new 值分配给同一个 threadHandles 变量。您失去了以前的值,因此失去了等待该线程的能力(这也会产生编译器警告,这是您问题的主题)。
  • 您将 相同 info 值传递给每个线程,从而泄漏您为它们分配的内存,并在它们之间造成数据竞争。

这是对上述问题的修复(可能还有其他我没有立即发现的问题):

pthread_t *threadHandles;
ThreadInfo *info;

threadHandles = calloc(totalElems, sizeof(pthread_t));
info = calloc(totalElems, sizeof(ThreadInfo));

for(index = 0; index < totalElems; index++) {
  info[index].threadNum = threadNum;
  // Initialize other elements of info here.
  pthread_create(&threadHandles[index], NULL, ThreadFunc, &info[index]);
}
// Wait for threads to finish
for(index = 0; index < totalElems; index++) {
  pthread_join(threadHanles[index], NULL);
}
// Free memory
free(threadHandles);
free(info);
return 0;