1
thread_local int sum = 0;   
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

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

int main()
{
    vector<vector<int>> *A = new vector<vector<int>>[3];
    vector<vector<int>> result;

    A[0].resize(50);
    A[1].resize(50);
    A[2].resize(50);
    for (int i = 0; i < 3; i++)
        for (auto &j : A[i])
            j.resize(50);

    result = A[0];
    for (int i = 1; i < 3; i++)
        calculate(result, A[i]);

}

If Sleep(1000) call is not present, this code works as expected.

If Sleep(1000) call is present, the calculate() function works first time, but second time, it terminates process when the loop around T.push_back ends.

Why this situation happens?

(Windows7 and microsoft visual studio 2015)

Sean
  • 60,939
  • 11
  • 97
  • 136
  • 2
    Your question is unclear. Please be more precise about what threads run and when, and on what conditions they terminate and how. – jotik Apr 26 '16 at 09:38
  • also explain what "sum" is, because you appear to have concurrent access to this variable. – Sven Nilsson Apr 26 '16 at 11:20
  • Cannot reproduce [Demo](http://coliru.stacked-crooked.com/a/8af64aa66d694172). – Jarod42 Apr 26 '16 at 12:30
  • I edit the code. and if size of A = [5 * 5], this code well activates but A = [50 * 50] doesn't activate well. – MultiiiiSleeping Apr 26 '16 at 14:24
  • I solve this problem because timers are over 1430 in my computer than terminates process. but i don't know why this happnes. – MultiiiiSleeping Apr 26 '16 at 14:56
  • 1
    Look at [maximum-number-of-threads-per-process-in-linux](http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux), as `50*50` threads is `2500` threads (whereas without sleep, thread has time to finish). – Jarod42 Apr 26 '16 at 23:06

1 Answers1

0

If I'm reading the code correctly, you're trying to run 2,500 threads simultaneously.

On a 32-bit process with default settings, the maximum number of threads you can possibly run simultaneously is 2,048. More than this, and you run out of virtual address space, causing the process to crash.

But even if you messed with the settings or recompiled as 64-bit, this is not a sensible approach; it will perform very poorly. Instead, use a work queue or some other technique to farm the work out to a smaller number of threads; you will typically get optimum performance if you have one thread for each CPU core.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158