pthread_create()的问题
所以我之前问了一个关于一个具体问题的问题。我看过这个网站上的其他问题,但大多数都没有涉及我的问题,特别是我认为这个问题对其他初学者很有用。这是密码 (pi.h)我的结构是如何布置的pthread_create()的问题,c,struct,pthreads,C,Struct,Pthreads,所以我之前问了一个关于一个具体问题的问题。我看过这个网站上的其他问题,但大多数都没有涉及我的问题,特别是我认为这个问题对其他初学者很有用。这是密码 (pi.h)我的结构是如何布置的 #ifndef __PI_TEST_H #define __PI_TEST_H #include <stdio.h> #include <stdlib.h> struct piS //struct for threading { int threa
#ifndef __PI_TEST_H
#define __PI_TEST_H
#include <stdio.h>
#include <stdlib.h>
struct piS //struct for threading
{
int threads; //amount of threads running
int iterations; //amount of iterations
int operation; //whether to add or subtract
double total; //value of pi
};
double calcPi(int numIter);
void *piPthreads(void *info);
#endif
我可以看到线程正在正常运行。我也能理解为什么它不在我的for循环中处理实际的计算。这其中大部分是指导老师向我们展示的代码,它只是真正涉及到了,但并没有被讨论过。我已经试着在pi.c中使用print语句来解决这个问题。我可以看到迭代值没有被正确地放置。从我对这段代码的了解来看,它不会按我希望的方式运行。但是我很难弄清楚为什么我的迭代没有通过 传递给
pthread\u create
的第四个参数是&threader
。此表达式具有类型struct piS**
,因为threader
具有类型struct piS*
。但是在线程函数中,您可以将参数转换为类型struct piS*
。这些不匹配,因此您试图将一种类型的对象视为另一种类型的对象。这引起了人们的注意
您应该将&threader[loop]
传递到pthread\u create
以便类型匹配
pthread_create(&tids[loop],NULL,piPthreads,&threader[loop]);
在
内部,如果(argc==3)
…您创建了新变量,而外部变量保持不变。如果为True,我将删除声明并只分配值。谢谢。另外,我认为pthread\u create
的最后一个参数是错误的。是不是应该是&threader[loop]
?@Johnnymapp dbush帮助解释了它,但感谢您指出它。谢谢。就像我说的,我们是从我们的教练那里得到的。我花了一点时间才明白它是如何变成双指针的。但你说的我又读了几遍。它更有意义,它传递了这些值。现在我只需要研究一下我的逻辑。非常感谢你。
//driver class to run pthread
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "pi.h"
/**********************************************
* main()
* handles command line arguments and passes
* for pThreads
**********************************************/
int main(int argc, char** argv){
int numIter = 100;
int pthread = 4;
//Handle command line args
if(argc != 3)
{
printf("Default:Number of threads being set to 4.\n");
printf("Default:Number of iterations set to 100.\n");
if(atoi(argv[2]) > 28)
{
printf("Default:Number of threads being set to 4\n");
printf("Max threads possible 28\n");
pthread = 4;
}
printf("Format for running:( executable iterations threads)\n");
}
if(argc == 3)
{
//Assign args
numIter = atoi(argv[1]);
//Assign threads
pthread = atoi(argv[2]);
}
//creating and handling pthreads
pthread_t *tids = (pthread_t*) malloc(sizeof(pthread_t) * pthread);
struct piS *threader = (struct piS*) malloc(sizeof(struct piS) * pthread);
int loop = 0;
//filling in values for pthreads and creating each thread
for(loop = 0;loop < pthread;loop++)
{
threader[loop].operation = loop;
threader[loop].threads = pthread;
threader[loop].iterations = numIter;
printf("threader[%d].iterations = %d\n",loop,threader[loop].iterations);
pthread_create(&tids[loop],NULL,piPthreads,&threader);
}
double answer = 0;
for(loop = 0;loop < pthread; loop++)
{
pthread_join(tids[loop],NULL);
answer += threader[loop].total;
printf("threader[%d] = %d\n",loop,threader[loop].total);
}
answer = answer * 4;
printf("answer is %f using %d iterations\n",answer,numIter);
return 0;
}
Default:Number of threads being set to 4.
Default:Number of iterations set to 100.
Format for running:( executable iterations threads)
threader[0].iterations = 100
threader[1].iterations = 100
threader[2].iterations = 100
iterations = 0
begin: 0, end: 0
threader[3].iterations = 100
iterations = 0
begin: 0, end: 0
threader[0] = 0
threader[1] = 1
iterations = 0
begin: 0, end: 0
iterations = 0
begin: 0, end: 0
threader[2] = 2
threader[3] = 3
answer is 0.000000 using 100 iterations
pthread_create(&tids[loop],NULL,piPthreads,&threader[loop]);