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:

1// DO NOT USE Type() 2HPS::Key generic_key; 3if (generic_key.Type() == HPS::Type::LineKey) 4{ 5 HPS::LineKey line_key = (HPS::LineKey) generic_key; 6 // do stuff with the LineKey 7}

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:

1For example, do this: 2HPS::Key generic_key; 3try { 4 HPS::LineKey line_key(generic_key); 5 // do stuff with the LineKey 6} 7catch (...) 8{ 9 // generic_key didn't exist, or wasn't actually a line. 10}

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.