# IThreadPoolService

`IThreadPoolService` in assembly SanteDB.Core.Api version 3.0.1980.0

## Summary

Represents a thread pooling service

## Operations

| Operation         | Response/Return | Input/Parameter                                                                                                                                              | Description                                      |
| ----------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------ |
| QueueUserWorkItem | void            | *Action\<Object>* **action**                                                                                                                                 | Queues the specified action into the worker pool |
| QueueUserWorkItem | void            | <p><em>Action\<TParam></em> <strong>action</strong><br><em>TParam</em> <strong>parm</strong></p>                                                             | Queue user work item                             |
| GetWorkerStatus   | void            | <p><em>Int32&</em> <strong>totalWorkers</strong><br><em>Int32&</em> <strong>availableWorkers</strong><br><em>Int32&</em> <strong>waitingInQueue</strong></p> | Get worker status                                |

## Implementations

### DefaultThreadPoolService - (SanteDB.Core.Api)

Represents a thread pool which is implemented separately from the default .net threadpool, this is to reduce the load on the .net framework thread pool

#### Description

Many SanteDB jobs use threads to perform background tasks such as refreshes, matching, job execution, etc. Because we don't want uncontrolled explosion of threads, we use a thread pool in order to control the number of active threads which are being used.

```
        Implementers may choose to register a [IThreadPoolService](http://santesuite.org/assets/doc/net/html/T_SanteDB_Core_Services_IThreadPoolService.htm) which uses the .NET [ThreadPool](https://docs.microsoft.com/en-us/dotnet/api/system.threading.threadpool) (see: [NetThreadPoolService](http://santesuite.org/assets/doc/net/html/T_SanteDB_Core_Services_Impl_NetThreadPoolService.htm)), 
        or they can choose to use this separate thread pool service. There are several advantages to using the SanteDB thread pool rather than the 
        .NET thread pool including:
        
```

* The default .NET thread pool can bounce work between threads when the thread enters a wait state, this can cause issues with the REDIS connection multiplexer
* SanteDB plugins may use PLINQ or other TPL libraries which require using .NET thread pool - and we don't want longer running processess consuming those threads
* Implementers may wish to have more control over how the Thread pool uses resources

This thread pool works by spinning up a pool of threads which wait for [Object})](#Object})) which initiates (or queues) a request to perform background work. When an available thread in the pool can execute the task, the task will be run and the thread will work on the next work item.

If the thread pool needs additional threads (i.e. there are a lot of items in the backlog) it will spin up new reserved threads at a rate of number of CPUs on the machine. This continues until the environment variable SDB\_MAX\_THREADS\_PER\_CPU is hit.

Conversely, if the thread pool threads remain idle for too long (1 minute) they are destroyed and removed from the thread pool. This ensures over-threading is not done on the host machine.

#### Service Registration

```markup
...
<section xsi:type="ApplicationServiceContextConfigurationSection" threadPoolSize="4">
	<serviceProviders>
		...
		<add type="SanteDB.Core.Services.Impl.DefaultThreadPoolService, SanteDB.Core.Api, Version=3.0.1980.0, Culture=neutral, PublicKeyToken=null" />
		...
	</serviceProviders>
```

### NetThreadPoolService - (SanteDB.Core.Api)

Represents a thread pool which is implemented to wrap .NET thread pool

#### Description

This class is a remnant / adaptation of the original thread pool service from OpenIZ because OpenIZ used PCL which didn't have a thread pool. Additionally it provided statistics on the thread pool load, etc. This has been refactored.

#### Service Registration

```markup
...
<section xsi:type="ApplicationServiceContextConfigurationSection" threadPoolSize="4">
	<serviceProviders>
		...
		<add type="SanteDB.Core.Services.Impl.NetThreadPoolService, SanteDB.Core.Api, Version=3.0.1980.0, Culture=neutral, PublicKeyToken=null" />
		...
	</serviceProviders>
```

## Example Implementation

```csharp
/// Example Implementation
using SanteDB.Core.Services;
/// Other usings here
public class MyThreadPoolService : SanteDB.Core.Services.IThreadPoolService { 
	public String ServiceName => "My own IThreadPoolService service";
	/// <summary>
	/// Queues the specified action into the worker pool
	/// </summary>
	public void QueueUserWorkItem(Action<Object> action){
		throw new System.NotImplementedException();
	}
	/// <summary>
	/// Queue user work item
	/// </summary>
	public void QueueUserWorkItem<TParam>(Action<TParam> action,TParam parm){
		throw new System.NotImplementedException();
	}
	/// <summary>
	/// Get worker status
	/// </summary>
	public void GetWorkerStatus(Int32& totalWorkers,Int32& availableWorkers,Int32& waitingInQueue){
		throw new System.NotImplementedException();
	}
}
```

## References

* [IThreadPoolService C# Documentation](http://santesuite.org/assets/doc/net/html/T_SanteDB_Core_Services_IThreadPoolService.htm)
* [DefaultThreadPoolService C# Documentation](http://santesuite.org/assets/doc/net/html/T_SanteDB_Core_Services_Impl_DefaultThreadPoolService.htm)
* [NetThreadPoolService C# Documentation](http://santesuite.org/assets/doc/net/html/T_SanteDB_Core_Services_Impl_NetThreadPoolService.htm)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.santesuite.org/developers/server-plugins/implementing-.net-features/service-definitions/ithreadpoolservice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
