c++ - 在 For 循环中创建多个线程来处理二维数组

标签 c++ multithreading pthreads posix multidimensional-array

好的,所以我的代码可能有点困惑 - 我是新手,提前致歉!基本上,代码将文件读入二维数组,然后创建多个线程来对数组的多行中的每个值执行计算。我遇到的问题是底部的 for 循环。它运行但不循环。我不知道为什么,也尝试了很多东西,但我是新手,所以老实说我不知道​​。所有代码都在下面,如果有任何帮助,我们将不胜感激!

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>

int threadArray[10][10];
int arrayVar[2];

using namespace std;

void *calc(void *arg){

  int startPoint = arrayVar[0];
  int endPoint = arrayVar[1];
  int newArray[10][10];
  int calculated;

  for (int i = startPoint ; i < endPoint; i++){
    for (int j = 0; j < 10; j++){
      calculated = (threadArray[i][j] * 2 + 4) * 2 + 4;
      newArray[i][j] = calculated;
    }
  }

  for (int i = startPoint; i < endPoint; i++){
      for (int j = 0; j < 10; j++){
        cout << newArray[i][j] << " ";
      }
      cout << endl;
   }
  return NULL;
}

int main(){

  int rc;
  int start = 0;
  int end;

  ifstream numFile;
  numFile.open("numbers.txt");

  if (numFile.is_open()){

    for (int row = 0; row < 10; row++){
      std::string line;
      std::getline(numFile, line);

      std::stringstream iss(line);

      for (int col = 0; col < 10; col++){
    std::string num;
    std::getline(iss, num, ' ');

    std::stringstream converter(num);
    converter >> threadArray[row][col];
      }

    }

    cout << "Original 2D Array" << endl << endl;
    for (int i = 0; i < 10; i++){
      for (int j = 0; j < 10; j++){
    cout << threadArray[i][j] << " ";
      }
      cout << endl;
    }

    cout << endl;

  }

    srand (time(NULL) );

    const int rowArray[3] = {1, 2, 5};

    int arrayIndex = rand() % 3;
    int noOfRows = (rowArray[arrayIndex]);
    end = noOfRows;
    int noOfThreads = 10 / noOfRows;

    pthread_t threads[noOfThreads];

    arrayVar[2];

    cout << "2D Array Altered" << endl << endl;

    for (int t = 0; t < noOfThreads; t++){
      arrayVar[0] = start;
      arrayVar[1] = end;
      rc = pthread_create(&threads[t], NULL, calc, NULL);
      start = start + noOfRows;
      end = end + noOfRows;
      pthread_exit(NULL);
    }
}

最佳答案

for (int t = 0; t < noOfThreads; t++)
{
  arrayVar[0] = start;
  arrayVar[1] = end;
  rc = pthread_create(&threads[t], NULL, calc, NULL);
  start = start + noOfRows;
  end = end + noOfRows;
  // pthread_exit(NULL);  <-- this EXITS the loop and main
}

把上面的pthread_exit注释掉,再来个循环

for (int t = 0; t < noOfThreads; t++)
{
    rc = pthread_join(threads[t], NULL);
}

pthread_exit(NULL);

关于c++ - 在 For 循环中创建多个线程来处理二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21082508/

相关文章:

c - 多线程应用程序在 Release模式下崩溃

c - 如何安排 pthread_cond_signal() 以使其始终跟随另一个线程上的 pthread_cond_wait()?

linux - pthread lock/unlock解决方案

c - Pthread 条件信号 - 未按预期工作

c++ 使用 winmain()

c++ - 如何在给定字符串中查找字母序列?

C++ 如何将 std::mutex 和 std::lock_guard 与仿函数一起使用?

c++ - 控制命名空间中的访问?

c++ - Clang 和 Intel 无法编译此 CRTP 代码

c# - 使用 Task.Factory.StartNew 新创建的线程启动非常慢