多线程程序;产生更多线程的线程

时间:2020-03-30 17:51:03

标签: c++ multithreading

我目前正在开发一个程序,该程序需要10000个随机数的数组,并用5个线程将​​其平均分配,然后将这5个线程进一步平均分配为20个线程。目的是从原始的10000个随机数数组中找到最终的最小数。

我相信自己的方向正确。经过进一步检查,我在级别1上的数组遍历有问题。我只能看到返回的前5个线程中的20个线程之一。返回的信息似乎是正确的,但是当我尝试对信息进行提示时,我得到的空白空间为L1线程的L2线程的返回最小值4。 (抱歉,这有点令人困惑。)

有人能给我一些有关寻找什么的提示吗?我将在下面提供代码。

请记住,效率不是此程序的目标。演示多线程是。

#include <sys/time.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <iostream>
#include <pthread.h>
#include <cstdlib>
#include <unistd.h>

/*
Macros were used to make modulation easier and to ease reading of the code
*/

#define L1_THREADS 5  //# of level 1 threads
#define L2_THREADS 20 //# of level 2 threads
//#define L1_ARRAY 5  //eventually unused
#define L2_ARRAY 2000 //size of level 2 array

using namespace std;

//structure to house the Thread Parameters required
struct ThreadParameters{
  int* array;
  int start;
  int end;
  int smallest;
};

//function to find the minimum value at the bottom level of the thread spread
void* find_min(void* args){
  struct ThreadParameters* specs = (struct ThreadParameters*)args;
  int *array = specs->array;
  int start = specs->start;
  int end = specs->end;
  int smallest = array[start];

  for(int i = start; i < end; i++){
    if(array[i] < smallest){
      smallest = array[i];
    }
  }

  specs->smallest = smallest;

  return NULL;
}

//function to find the minimum value of the values returned by the threads it creates
void* find_first(void* args){
  pthread_t threads2[L2_THREADS] = {0};
  struct ThreadParameters thread2_parameters[L2_THREADS] = {0};

  struct ThreadParameters* specs = (struct ThreadParameters*)args;
  int *array = specs->array;
  int start = specs->start;
  int end = specs ->end;
  int smallest = array[start];

  //Level 1, creates the 20 threads for level 2
  for(int i = 0; i < L2_THREADS; i++){
    thread2_parameters[i].array = array;
    thread2_parameters[i].start = i*(L2_ARRAY/L2_THREADS);
    thread2_parameters[i].end = (i+1)*(L2_ARRAY/L2_THREADS);
    pthread_create(&threads2[i], NULL, find_min, &thread2_parameters[i]);
  }

  for(int i = 0; i < L2_THREADS; i++){
    pthread_join(threads2[i], NULL);
  }

  cout << "Minimums from L2 threads: ";

  for(int i = start; i < L2_THREADS; i++){
    cout << "[" << thread2_parameters[i].smallest << "]";
    if(thread2_parameters[i].smallest < smallest){
      smallest = thread2_parameters[i].smallest;
    }
  }

  cout << endl;

  specs->smallest = smallest;

  return NULL;
}

int main(){

  time_t t;
  int n = 10000;
  int randnum[n];    /* array of random numbers */

  /* Initialize random number generator */
  srand((unsigned) time(&t));

  /* Generate random numbers */
  for (int i = 0; i < n; i++) {
    randnum[i] = rand()%10000 +1;
  }

  /*  end of random number generation code */

  pthread_t threads1[L1_THREADS] = {0};
  struct ThreadParameters thread1_parameters[L1_THREADS] = {0};
  //int min[L1_ARRAY];

  int smallest;
  smallest = randnum[0];

  //Level 0, creates the first five threads for level 1
  for(int i = 0; i < L1_THREADS; i++){
    thread1_parameters[i].array = randnum;
    thread1_parameters[i].start = i*(n/L1_THREADS);
    thread1_parameters[i].end = (i+1)*(n/L1_THREADS);
    pthread_create(&threads1[i], NULL, find_first, &thread1_parameters[i]);
  }

  for(int i = 0; i < L1_THREADS; i++){
    pthread_join(threads1[i], NULL);
  }

  cout << "Minimums from L1 threads: ";

  //finds the ultimate minimum after L1 threads return with their values
  for(int i = 0; i < L1_THREADS; i++){
    cout << "[" << thread1_parameters[i].smallest << "]";
    if(thread1_parameters[i].smallest < smallest){
      smallest = thread1_parameters[i].smallest;
    }
  }

  cout << "\nThe ultimate minimum is " << smallest << endl;

  return 0;

}

0 个答案:

没有答案
相关问题