Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
'var' : l-value use of initonly data member is not allowed directly within a parallel region in class 'class'
An initonly (C++/CLI) data member cannot be initialized inside that part of a constructor that is in a parallel region. This is because the compiler does an internal relocation of that code, such that, it is effectively no longer part of the constructor.
To resolve, initialize the initonly data member in the constructor, but outside the parallel region.
The following sample generates C3899.
// C3899.cpp
// compile with: /clr /openmp
#include <omp.h>
public ref struct R {
initonly int x;
R() {
x = omp_get_thread_num() + 1000; // OK
#pragma omp parallel num_threads(5)
{
// cannot assign to 'x' here
x = omp_get_thread_num() + 1000; // C3899
System::Console::WriteLine("thread {0}", omp_get_thread_num());
}
x = omp_get_thread_num() + 1000; // OK
}
};
int main() {
R^ r = gcnew R;
System::Console::WriteLine(r->x);
}
Please sign in to use this experience.
Sign in