From GridLAB-D Wiki
Jump to: navigation, search

gld_wlock - C++ Module API scope write lock Template:NEW30


#include <gridlabd.h>


Scope write locks on objects may be taken using the gld_wlock class. As long as the lock is in scope, the object remain write-locked. When the lock goes out of scope the object is unlocked.


The following example implements a scope write-lock for an if-else statement, thereby avoiding multiple unlock calls for each return.

{ gld_wlock lock(my()); 
  // object is now write-locked
  value1 = 12.3;
  value2 = 45.6;
} // object unlocks when lock goes out of scope


Be careful not to use general purpose accessors inside scope locks. This will cause a deadlock. If you need to set or get a property while a scope lock is held then use the unlocked accessors, e.g.,

gld_property prop(obj,"varname"); // access the object's property
if ( prop.is_valid() ) // verify that the property is valid
  double value; // create local space for the value
  gld_wlock lock(obj); // lock the object
  prop.getp(value,lock); // get the old value
  value+=12.3; // modify the value
  prop.setp(value,lock); // set the new value
} // unlock is automatic when lock goes out of scope


Scope locks are supported as of Hassayampa (Version 3.0).

See also