OpenMP: Access violation after finishing the parallel section

I’m working on a project to deal with a massive matrix, and I tried to use OpenMP to accelerate the procession. But it keeps failing with access violation. I then used a simplified version for testing, here is the code for testing (unnecessary codes omitted).

int main(void)
{
    int arr[10];
    omp_set_num_threads(10);
#pragma omp parallel shared(arr)
    {
        int id = omp_get_thread_num();
        arr[id] = id;
        printf("Thread id: %dn", id);
    }
printf("Finished!");
    return 0;
}

What makes me confused is that, even the threads are definitely reading and writing on different address, the program fails because of access violation every time, and according to the output, the violation happens once the program finished the parallel section and is about to leave.

Source: Windows Questions C++

LEAVE A COMMENT