创建线程以在阵列上进行计算 - 似乎无法正确访问数组

时间:2014-01-13 19:27:28

标签: c++ multithreading pthreads argument-passing

好的,所以我将一个参数传递给一个线程,我用它来告诉它在数组中查找的位置。我已经摆脱了之前被抛出的分段错误,但它仍然没有像我想的那样工作。传递给pointer的int似乎在某个地方发生了变化。这会抛出整个阵列,我会得到真正随机的数字。如果有任何可能的方式,任何人都可以帮我解决这个问题,我将不胜感激。我想要的代码就是读取2d数组,选择一个随机数(1,2或5)来决定一个线程将处理多少行数,然后创建一些线程来处理数组的行。这可能是错误的方法,但我不知道如何做到这一点。这应该是帮助我了解线程等的课堂作业,但到目前为止,我认为这让我更加困惑!如果可以,请帮忙!

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

using namespace std;

void *calc(void *pointer){

  int *point, pointerA;
  point = (int *) pointer;
  pointerA = *point;

  int startPoint = arrayVar[pointerA][0];
  int endPoint = arrayVar[pointerA][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 0;
}

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[noOfThreads][2];
    start = 0;
    end = noOfRows;

    for (int a = 0; a < noOfThreads; a++){
      arrayVar[a][0] = start;
      arrayVar[a][1] = end;
      start = start + noOfRows + 1;
      end = end + noOfRows + 1;
    }

    int *pointer = 0;

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

    for (int t = 0; t < noOfThreads; t++){
      pointer = (int *) t;
      rc = pthread_create(&threads[t], NULL, calc, &pointer);
    }

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

1 个答案:

答案 0 :(得分:1)

这是你的问题:

for (int t = 0; t < noOfThreads; t++) {
  pointer = (int *) t;
  rc = pthread_create(&threads[t], NULL, calc, &pointer);
}

您正在将指向pointer的指针传递给pthread_create,并在它超出循环体末端的范围时立即销毁它。当创建的线程开始运行并取消引用该指针时,会出现未定义的行为。

您需要吃掉一些灰色区域行为并将void*参数中的整数编码为pthread_create

void *calc(void *pointer) {
  int t = reinterpret_cast<int>(pointer);  
  // ...
}

// snip

for (int t = 0; t < noOfThreads; t++) {
  rc = pthread_create(&threads[t], NULL, calc, reinterpret_cast<void*>(t));
}

或动态分配存储以将整数传递给线程函数。