hexed 0.4.0
 
Loading...
Searching...
No Matches
hexed::Lock Class Reference

wrapper for OpenMP nested lock routines. More...

#include <Lock.hpp>

Classes

class  Set
 sets the lock when constructed and unsets when destroyed More...
 

Public Member Functions

 Lock (const Lock &)
 
void operator= (const Lock &)
 
std::optional< Settest ()
 If the lock is available, set it and return a Set object. Otherwise, return empty std::optional.
 
bool is_set ()
 Returns true if the lock is currently set, but leaves it in the same state.
 

Detailed Description

wrapper for OpenMP nested lock routines.

This class can be used to prevent data races by OpenMP threads. It contains an OpenMP lock variable. To set the lock, construct a Set object from it. When the Set object is destroyed, the lock will be unset. If Hexed is not compiled with OpenMP, this class does nothing. Use like this:

Lock l;
#pragma omp parallel for
for (int i = 0; i < N; ++i) {
Lock::Set s(l); // sets lock
// only one thread at a time can execute any statements here
} // lock is released because `s` is destroyed
sets the lock when constructed and unsets when destroyed
Definition Lock.hpp:37
wrapper for OpenMP nested lock routines.
Definition Lock.hpp:31
Warning
The copy constructor and assignment operator (Lock(const Lock&) and Lock::operator=(const Lock&)) are not thread-safe! You may not copy a Lock that is currently owned by another thread.

Constructor & Destructor Documentation

◆ Lock()

hexed::Lock::Lock ( const Lock & )
Todo
make this thread safe!

Member Function Documentation

◆ is_set()

bool hexed::Lock::is_set ( )

Returns true if the lock is currently set, but leaves it in the same state.

Effectively equivalent to bool set; {set = lock.test().has_value();}.

◆ operator=()

void hexed::Lock::operator= ( const Lock & )
Todo
make this thread safe!

◆ test()

std::optional< Lock::Set > hexed::Lock::test ( )

If the lock is available, set it and return a Set object. Otherwise, return empty std::optional.

Note
Doesn't block if the lock isn't available.

The documentation for this class was generated from the following files: