【问题标题】:Implement Multithreading in loop在循环中实现多线程
【发布时间】:2020-06-12 22:19:30
【问题描述】:

我正在编写一个应用程序,该应用程序使用不同的线程号运行 Monaco 算法,它们是 2、4、6 和 8 来计算 PI 的值。目标也是在使用更多线程时提高速度。问题是,我的代码不是分工,而是简单的做工2次!

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> 
#include <time.h> 
#include <math.h>
#include <string.h>
#define SEED 35791246


void *monaco(int arcg, char* argv) {

  clock_t t1, t2;
  float pi;
  t1 = clock();
  t2 = clock();


  float diff = ((float)(t2 - t1) / 1000000.0F );
  printf("\n");
  printf("Thread number = %d\n", (int)(pthread_self()));
  printf("Total Time: %f segundos\n", diff);
  printf("\n");
  printf("\n");
  return 0;
}


void run_test(int num_thread) {
  pthread_t pth_arr[num_thread];
  int i = 0;
  for (i = 0; i < num_thread; i++) {
    pthread_create(&pth_arr[i], NULL, monaco, NULL);
  }

  for (i = 0; i < num_thread; i++) {
    pthread_join(pth_arr[i], NULL);
  }
}


int main(count) {
  int num_thread = 9;
  int pontos=20000;
  double x,y;
  count=0; 
  double z;
  float pi;
  int j = 0;
  int i = 0;
  srand(SEED);
  count = 0;
  for (i = 1; i < num_thread; i++) {
    if (i==1) 
    {
      continue;
    }
    if (i==3) 
    {
      continue;
    }
    if (i==5) 
    {
      continue;
    }
    if (i==7) 
    {
      continue;
    }
      for (j = 0; j<pontos; j++) {
        x = (double)rand()/RAND_MAX;
        y = (double)rand()/RAND_MAX;
        z = x*x+y*y;
        if (z<=1) count++;
      }
      pi=(double)count/pontos * 4;
      printf ("Points used are: %d , and the Pi estimate is: %g \n", pontos,pi);
    printf ("Using %d threads.\n", i);
    run_test(i);
  }
  return 0;
}

我得到的输出是:

Points used are: 20000 , and the Pi estimate is: 3.1288                                                                          
Using 2 threads.                                                                                                                          

Thread number = -1332082944                                                                                                                  
Total Time: 0.000002 segundos                                                                                                                  



Thread number = -1340475648                                                                                                                  
Total Time: 0.000002 segundos 

Thread number = -1340475648                                                                                                                  
Total Time: 0.000002 segundos                                                                                                                  


Points used are: 20000, and the Pi estimate is: 6.2932                                                                          
Using 4 threads.                                                                                                                          

Thread number = -1332082944                                                                                                                  
Total time: 0.000002 segundos                                                                                                                  


Thread number = -1348868352                                                                                                                  
Total time: 0.000001 segundos                                                                                                                  


Thread number = -1357261056                                                                                                                  
Total time: 0.000001 segundos                                                                                                                  



Thread number = -1340475648                                                                                                                  
Total time: 0.000002 segundos                                                                                                                  


Points used are: 20000 , and the Pi estimate is: 9.4232

我怎样才能让它分工而不是做2次?

【问题讨论】:

    标签: c multithreading for-loop pthreads


    【解决方案1】:
    • 编译器警告

    在学习的早期阶段(即使您是专家),您需要假设编译器比您更聪明。当我按原样编译您的代码时,出现以下错误:

    SO.c: In function ‘run_test’:
    SO.c:32:39: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]
       32 |     pthread_create(&pth_arr[i], NULL, monaco, NULL);
          |                                       ^~~~~~
          |                                       |
          |                                       void * (*)(int,  char *)
    In file included from SO.c:3:
    /usr/include/pthread.h:236:15: note: expected ‘void * (*)(void *)’ but argument is of type ‘void * (*)(int,  char *)’
      236 |       void *(*__start_routine) (void *),
          |       ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
    SO.c: In function ‘main’:
    SO.c:41:5: warning: type of ‘count’ defaults to ‘int’ [-Wimplicit-int]
       41 | int main(count) {
          |     ^~~~
    

    在第一组错误中,编译器报告了不兼容的指针参数。从 pthreads 手册页:

    int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                              void *(*start_routine) (void *), void *arg);
    

    pthread_create()void* (*func) (void*) 函数指针作为参数,但您传递的monaco() 函数是void* (*monaco) (int, char*) 类型。

    如果您想将多个参数传递给start_routine(),您需要考虑一些替代方法,例如,将它们打包到一个结构中并将其地址作为参数传递。

    关于第二组错误,我很惊讶地看到您的代码首先编译。我不知道这种声明的含义,其他人需要回答:(

    • 逻辑

    即使在纠正了所有语法错误之后,我也不确定您的代码将如何在线程之间分配工作。您正在 main() 中进行大部分计算。也许你在代码的初始版本中,我不想通过提供完整代码来破坏乐趣:)

    对于错误部分,对于每次迭代,值加倍,因为您没有在迭代开始时将 count 重新初始化为 0。有了这一行,count = 0;for 循环的开头,输出与预期一样,但请记住,您的代码在当前状态下是不可接受的。

    提示:i &amp; 1 是一种确定奇数的简单方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 2014-11-29
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多