testcase b.C attached to show the problem. "a.ai += addnum" refers to distinct objects therefore a.ai finally equals to 7 rather than 28 (now it is!) when reaching printf. micro# g++ b.C -fopenmp micro# ./a.out constructor constructor constructor constructor constructor ai: 28 exp: 7 af: 28.000000 exp: 7 micro# g++ -v Using built-in specs. Target: sparc-sun-solaris2.10 Configured with: /import/dr2/starlex/orig/trunk/configure --prefix=/import/dr3/s10/gcc-4.3/ --enable-languages=c,c++,fortran --disable-gnattools --with-mpfr=/ws/gccfss/tools --with-gmp=/ws/gccfss/tools Thread model: posix gcc version 4.3.0 20070912 (experimental) (GCC)
Created attachment 14789 [details] C++ OpenMP testcase
IMHO it can validly print 7, 14, 21 or 28. See OpenMP 2.5, section 2.5.2: "The method of scheduling the structured blocks among threads in the team is implementation defined." Also, data sharing clause is sections construct clause rather than section directive clause, so no matter how many section directives you have, the constructor/destructor will be called as many times as there are threads in the team. GCC schedules the first not yet processed #pragma omp section block to the first available thread, doesn't do assign different blocks to different threads no matter whether they are available or not. So, if e.g. one (typically master) thread is available some time before all other threads and there isn't really any significant work to do in the block, so it finishes almost instantly, it might very well win the next section block as well, because other threads are still being set up.