c - pthread_create() 的问题

标签 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 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

(pi.c) 如何将值放入结构体并计算 pi

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <pthread.h>
#include "pi.h"

/***************************************************
 * struct for pthreads
***************************************************/

void *piPthreads(void *arg)
{
    struct piS *threader = (struct piS*) arg;
    int operation = threader->operation;    //rank
    int threads = threader->threads;    //count
    int iterations = threader->iterations / threads;
    double begin = operation * iterations;
    double end = begin + iterations;
    printf("iterations = %d\n",iterations);
    threader->total = 0;
    int plusMinus = 1;

    if((int)begin%2)
        plusMinus = -1;

    double loop = begin;

    printf("begin: %d, end: %d\n",begin, end);

    for(loop = begin;loop < end;loop++)
    {
        if(plusMinus == 1)
        {
            threader->total += (1/(2*loop+1));
            printf("threader->total = %d\n",threader->total);
        }
        else
            {
                threader->total -= (1/(2*loop+1));
                printf("threader->total = %d\n",threader->total);
            }
        } 

        return NULL;
    }

(pithreadDriver.c) 我如何运行 pthread 及其处理方式

//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;
}

(out.txt) 运行时的输出

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

我可以看到线程正在正常运行。我还可以明白为什么它不进入我的 for 循环来解决实际计算。其中大部分是讲师向我们展示的代码,只是真正触及过,但没有复习过。我尝试在 pi.c 中使用 print 语句来找出答案。我可以看到迭代值没有正确放置。从我得到的这段代码来看,它不会按照我希望的方式运行。但我无法弄清楚为什么我的迭代没有被通过。

最佳答案

传递给 pthread_create 的第四个参数是 &threader 。该表达式的类型为 struct piS **threader类型为struct piS * 。但在您的线程函数中,您将参数转换为类型 struct piS * 。它们不匹配,因此您尝试将一种类型的对象视为另一种类型的对象。这会调用 undefined behavior .

您应该通过&threader[loop]pthread_create所以类型匹配。

pthread_create(&tids[loop],NULL,piPthreads,&threader[loop]);

关于c - pthread_create() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52448025/

相关文章:

c - 如何在 Linux 中检测是否有任何其他实体/进程正在写入文件?

JSON 未解码为结构( fatal error ,Swift)

c - 在 C 中制作二维字符串数组

C++初学者声明涉及结构数组的函数

c++ - 将结构从 C++ dll 返回到 Python

c++ - Pthread_create 导致段错误(C++,Kubutnu 15)

从多线程程序调用 system()

c++ - 带有 pthreads 和 mutexes 的 OpenCV

c - Shellcode 未运行

c - 从 C 中机器代码形式的文本文件中读取字节?