void calculate(vector<vector<int>> &A, const vector<vector<int>> &B)
{
vector<vector<int>> temp;
vector<thread> T;
int row = A.size(), column = B[0].size(), n = A[0].size();
cout << "check print" << endl;
temp.resize(row);
for(auto &i : temp)
i.resize(column);
for (int i = 0; i < row; i++)
for (int j = 0; j < column; j++)
T.push_back(thread(thrCal, i, j, n, ref(temp[i][j]), ref(A), ref(B)));
for (auto &i : T)
i.join();
A = move(temp);
} //matrix A = A * B with multithread
void thrCal(int row, int column, int n, int &result, const vector<vector<int>> &A, const vector<vector<int>> &B)
{
Sleep(1000);
for (int i = 0; i < n; i++)
sum += A[row][i] * B[i][column];
result = sum;
} //calculates each element with thread
If Sleep(1000) function doesn't exist, this code activates well. If Sleep(1000) function exists, calculate function activates first time, but when second time, it terminates process when T.push_back loop end. Why this situation happens?
Aucun commentaire:
Enregistrer un commentaire