C++ 多线程程序;生成更多线程的线程

C++ 多线程程序;生成更多线程的线程,c++,multithreading,C++,Multithreading,我目前正在开发一个程序,它接受10000个随机数的数组,用五个线程将其平均分割,这五个线程进一步平均分割成20个线程。其目的是从原始10000个随机数数组中找到最终的最小数 我相信我在正确的轨道上。经过进一步检查,我在1级上的数组遍历有问题。我只能看到前5个线程的20个线程中的一个返回。返回的信息看起来是正确的,但是当我试图在信息发生时读取信息时,我得到了L1线程的L2线程中4个最小返回值的空白。(如果这有点混乱,很抱歉。) 有谁能给我一些关于注意什么的建议吗?我将在下面提供代码 请记住,效率不

我目前正在开发一个程序,它接受10000个随机数的数组,用五个线程将其平均分割,这五个线程进一步平均分割成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;

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
/*
宏被用来使调制更容易,也便于代码的读取
*/
#定义级别1线程的L1_线程5/#
#定义2级线程的L2_线程20/#
//#定义L1_数组5//最终未使用
#定义L2_数组2000//级别2数组的大小
使用名称空间std;
//用于容纳所需螺纹参数的结构
结构线程参数{
int*数组;
int启动;
内端;
int最小;
};
//函数查找螺纹排列底部的最小值
void*find_min(void*args){
struct ThreadParameters*规格=(struct ThreadParameters*)参数;
int*array=specs->array;
int start=specs->start;
int end=规格->结束;
int最小=数组[开始];
for(int i=start;i最小=最小;
返回NULL;
}
//函数来查找它创建的线程返回的值的最小值
void*首先查找(void*args){
pthread_t threads2[L2_THREADS]={0};
结构ThreadParameters thread2_参数[L2_线程]={0};
struct ThreadParameters*规格=(struct ThreadParameters*)参数;
int*array=specs->array;
int start=specs->start;
int end=规格->结束;
int最小=数组[开始];
//级别1,为级别2创建20个线程
对于(int i=0;i通常你有多少个内核?通常有2X线程作为一个很好的限制,并且任何东西都有减少的回报,通常是很严重的。你使用哪种C++版本?因为C++ 11有代码> STD::线程< /代码>,它更容易使用,例如你不必乱搞<代码> Value*/Cord>。对于我来说,100个线程对应10000个数字听起来太多了。那么多线程的开销和簿记很容易淹没计算本身。感谢这么多的快速响应。程序的效率并不重要。这个程序的目的是演示多线程,使用10000个数并像cr一样拆分它azy很有趣,至少可以这么说,虽然它很浪费,但它展示了在需要时这样做的能力。你也可以用两个线程演示多线程。只需在循环中打印线程ID。说到线程,linux上的线程和进程如何区分?你有多少内核?通常有多少内核2x线程是一个很好的限制,任何超出它的回报都是很小的。你使用哪种C++版本?因为C++ 11有<代码> STD::线程< /代码>,它更容易处理,例如,你不必乱搞<代码> Value>代码>,我的头上,100个线程,用于10000个数字的声音。太多了。那么多线程的开销和簿记很容易淹没计算本身。感谢这么多的快速响应。程序的效率并不重要。这个程序的目的是演示多线程,使用10000个数字并疯狂地拆分它,至少可以说是有趣的,也是浪费事实是这样的,但它展示了在需要时这样做的能力。你也可以用两个线程演示多线程。只需在循环中打印线程ID。说到线程,linux上的线程和进程如何区分?