[Back to Blog](/blog) # Work with Multi-Threading

Vadim Asandiy May 26, 2023

<a class="a2a_button_facebook"></a><a class="a2a_button_twitter"></a><a class="a2a_button_linkedin"></a>

For multi-threaded loading and regeneration, BimNv SDK uses the Intel® oneAPI Threading Building Blocks (oneTBB) library.

### Multi-Threaded Loading

By default, the thread count is obtained from the ODA thread pool. But if you need to set the thread count while loading files in multi-thread mode, override the method virtual int numThreads(OdDb::MultiThreadedMode mtMode) from OdDbBaseHostAppServices. For example:

```
//…
class MyServices : public OdNwHostAppServices
{
  //…  
  //method for setting thread count
  void setNumThreads(int numThreads)
  {
    m_numThreads = numThreads;
  }

  // override method for getting thread count
  int numThreads(OdDb::MultiThreadedMode mtMode) override
  {
    int numThrs = 1;
    bool bEnabled = false;
    switch (mtMode)
    {
      case OdDb::kMTLoading:
      bEnabled = GETBIT(getMtMode(), 1);
      break;
    }
    if (bEnabled)
      numThrs = m_numThreads;
      return numThrs;
  }

  protected:
    int m_numThreads;
};
//…
```

To open a file in multi-thread mode, use following code:

```
//…
OdNwHostAppServices* pHostApp;
OdString nwFile;
//…
OdInt16 nMode = pHostApp->getMtMode();

//set mt loading mode
SETBIT_1(nMode, 1);
pHostApp->setMtMode(nMode);
MyServices* pMyHostApp = dynamic_cast<MyServices*>(pHostApp);

//if our pHostApp contain override method for thread count's getting – then set 8 threads, for example
if (pMyHostApp != nullptr)
  pMyHostApp->setNumThreads(8);
OdNwDatabasePtr pDb = pHostApp->readFile(nwFile);
//… 
```

The following table shows the time decrease in multi-threaded loading when compared with single-threaded mode:

| | Partial loading | Partial loading | Partial loading | Full loading | Full loading | Full loading |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| | st vs mt2 | st vs mt4 | st vs mt8 | st vs mt2 | st vs mt4 | st vs mt8 |
| Average <br> improvements | -9% | -20% | -16% | -7% | -35% | -38% |

### Multi-Threaded Regeneration

For multi-threaded regeneration while using Gi/Gs vectorization, you can set the thread count as a GS device property:

```
//...
OdNwHostAppServices* pHostApp;
OdGsDevicePtr pDevice;
OdString nwFile;
OdUInt32 mtRegenThreadCnt;
//...
OdNwDatabasePtr pDb = pHostApp->readFile(nwFile);
//...
// Initialize GsDevice and set additional properties to GiContext
//...
OdRxDictionaryPtr devProperties = pDevice->properties();
devProperties->putAt(OD_T("EnableMultithread"), OdRxVariantValue(true));
devProperties->putAt(OD_T("MaxRegenThreads"), OdRxVariantValue(static_cast<OdUInt16>(mtRegenThreadCnt)));
//…
//now update of Gs device is going in mt-regen mode
pDevice->update();
//...
```

For off-screen vectorization devices, set the GS device property ForcePartialUpdate to true:

```
//…
devProperties->putAt(OD_T("ForcePartialUpdate"), OdRxVariantValue(true));
//…
```

The following table shows the time decrease in multi-threaded regeneration when compared with single-threaded mode:

| | st vs mt2 | st vs mt4 | st vs mt8 |
| :---: | :---: | :---: | :---: |
| Average <br> improvements | -28% | -45% | -42% |

The following hardware was used to get the statistics:

- Intel® Core™ i7-6700 CPU @ 3.40GHz; 1 socket, 4 cores, 8 logical processors
- 32GB RAM, speed 2133MHz
- HDD

Find out more in [our documentation](https://docs.opendesign.com/bimnv/bimnv_working_with_multi-threading.html).

## Get started today

**Try ODA software free for 60 days.  
No risk, no credit card required.**

[Try for Free](https://www.opendesign.com/free-trial)
