gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:14 24 4
gpt4 key购买 nike

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

错误...

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 中的成员。我完全像我的其他程序一样这样做,但它不起作用。我试过搜索网站和谷歌,但也许我无法弄清楚,因为它太具体了?此外,删除 & 也无济于事。任何帮助将不胜感激!

最佳答案

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 个线程,然后不等待其中任何一个就返回。一旦 main 返回,所有 线程都会随之消失(整个程序终止)。
  • 在循环的每次迭代中,pthread_create 都会将一个 值分配给同一个threadHandles 变量。你失去了以前的值(value),因此失去了等待那个线程的能力(这也会产生编译器警告,这是你问题的主题)。
  • 您将相同 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;

关于c - 与 pthread_create 不兼容的指针类型 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43958340/

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