FAQS - HPS - using multiple threads to access HPS

HPS is completely thread- safe. Everything is synchronized internally. That is a fundamental behavior of HPS.

All Keys, including SegmentKeys are 100% thread-safe. All Database operations, including Updates, are 100% thread-safe. You even have the choice of performing Update Synchronously (with Notifier() and Wait()) or Asynchronously.

HPS API can be accessible from multiple threads. If you plan on using multiple threads, please keep the following in mind:

  1. Avoid calling .Type() function:

For example:

// DO NOT USE Type()
HPS::Key generic_key;
if (generic_key.Type() == HPS::Type::LineKey)
{
    HPS::LineKey line_key = (HPS::LineKey) generic_key;
    // do stuff with the LineKey
}

Type() is expensive and will slowdown performance. Type() has to synchronize the database in order to know the true type of the Key. When you call Type(), all database operations are paused until the internal task queue is emptied. Only after the task queue is emptied can HPS determine the precise type of the Key and report that back to the user.

Instead, cast it to the type you expect.
For example:

For example, do this:
HPS::Key generic_key;
try {
    HPS::LineKey line_key(generic_key);
    // do stuff with the LineKey
}
catch (...)
{
    // generic_key didn't exist, or wasn't actually a line. 
}

2. Deleting Keys

You may need to pay special attention if you are deleting any Keys. If you delete a Key, the next time it’s used (perhaps in a different thread), it will throw an exception. If there are multiple threads in play, you will need to add exception handling for deleted Keys.

3. Kits are not thread-safe

One thing that is NOT thread-safe is Kits. If you’re passing or sharing kits across threads, you will need to synchronize them on your end.