在循环中连接线程 - 转换错误

时间:2014-01-13 09:26:28

标签: c++ multithreading compiler-errors pthreads pthread-join

所以我试图在for循环中加入线程,但是它给了我错误:

  

从'pthread_t * {aka long unsigned int *}'无效转换为   'pthread_t {aka long unsigned int}'。

代码如下,任何帮助将不胜感激! 提前谢谢!

#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;
    }

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

1 个答案:

答案 0 :(得分:2)

我认为线程[t]实际上只是它的pid,它是一个整数,你应该传入值

pthread_join(threads[t], NULL)