On Sat, Jun 20, 2009 at 06:42:22AM -0700, Tim Prince wrote: > Peter Cech wrote: > > > I was adding some OpenMP paralellization into an existing code and run > > into a puzzling problem. Here is a snippet to illustrate: > > > > // Inside a parallel for loop > > > > // The code statement below modifies a shared data structure > > #pragma omp critical > > Class instance = create_and_register_new_instance() > > > > // Problem here - compiler claims that "instance" was not declared in > > // this scope > > instance.do_something() > > > > So it seems that g++ 4.3 turns the code into: > > > > > > // The code statement below modifies a shared data structure > > #pragma omp critical > > { > > Class instance = create_and_register_new_instance() > > } > > > > // "instance" not declared here > > instance.do_something() > > > > This looks right. If you wish the critical region to include more than a > single statement, you need an explicit block with {}. Ah, sorry, I was not very clear. I should not have been lazy to prepare proper example code. here is something you can feed to a compiler directly: #include <map> // Just a dummy class with a method class Class { public: Class() {} void do_something() { // some computations here return; } }; // something that modifies shared/global data structure Class & create_and_register_new_instance() { static std::map<unsigned int, Class> registry; return registry[registry.size()]; } void loop1() { for (int i = 0; i < 10; ++i) { Class instance = create_and_register_new_instance(); instance.do_something(); } } void loop2() { # pragma omp parallel for for (int i = 0; i < 10; ++i) { # pragma omp critical Class instance = create_and_register_new_instance(); instance.do_something(); } } If I do not enable OpenMP, it compiles fine (just warning about ignoring the pragmas): $ g++ -W -Wall openmp.cpp -c openmp.cpp:34: warning: ignoring #pragma omp parallel openmp.cpp:36: warning: ignoring #pragma omp critical But if I enable OpenMP it does not compile anymore: $ g++ -W -Wall openmp.cpp -c -fopenmp openmp.cpp: In function 'void loop2()': openmp.cpp:39: error: 'instance' was not declared in this scope I would expect a clean compilation in both cases. Thanks, Peter