PinkFloyd
PinkFloyd

Reputation: 2193

c++ pragma omp critical(name)

Imagine I have a class method that contains a critical region :

class A{
    public:
        A(){};

        method(){
            #pragma omp critical(name)
                { //do something }
        }
}

Now I have two instances of A, ie. A a1 and A a2. What is the behaviour of a1.method() and a2.method() ? Can {//do something} be executed at the same time ?

Ultimatly, I want to forbid a simultaneous call of {//do something} on the same instance but to allow a simultaneous call of {//do something} on different instance.

Upvotes: 3

Views: 4398

Answers (1)

Lubo Antonov
Lubo Antonov

Reputation: 2318

The critical section is the same for all instances of the object - only one thread at a time will be able to enter.

To allow different instances to manage access separately, use a mutex owned by the instance. You can use the omp_init_lock() and the other omp_xxx_lock() functions, std::mutex, or another mutex implementation.

Upvotes: 1

Related Questions