【发布时间】:2017-05-13 21:53:37
【问题描述】:
我正在做一个使用多线程拆分数组、搜索目标并返回匹配数的个人项目。我的早期代码有一个错误和一些错误。
错误...
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
头文件...
#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 中的成员。我这样做就像我的其他程序一样,但它不起作用。我试过搜索网站和谷歌,但也许我无法弄清楚,因为它太具体了?此外,删除 & 也无济于事。任何帮助将不胜感激!
【问题讨论】:
-
note: expected 'pthread_t * restrict' but argument is of type 'pthread_t **'这不是一个非常清楚的信息,可以准确地告诉您问题所在吗? -
这不是您的任何问题,但关键字
auto已过时,并且多年来一直没有任何效果。我想这可能是我曾经第一次看到有人在 C 程序中使用它。 -
@kaylum 这是否意味着“线程句柄”上没有 &?因为如果我把它拿走,它说线程句柄是未初始化的。我在其他程序中将它作为“&threadhandles”使用,但在这个程序中不起作用。我将值读入 numSegElems 中,但为了不让我的帖子过大而将其留在这里。
-
您需要将其声明为:
pthread_t threadHandles[totalElems];,然后将其作为&threadHandles[index]传递给pthread_create。并且请不要发布不完整的代码。您需要创建一个minimal reproducible example。 -
@zwol 上学期跟一位老教授上课,坚持使用自动存储课,现在已经习惯了。