# qualpal::Threads

C++ · 4.1.0

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/threads.h>)

```cpp
class qualpal::Threads
```

Centralized management of thread settings for parallel computations. 

The [Threads](<00c82e3786a929271b2b4c19118c342df1627c8d3a932179e801db820fbafdea>) class provides a unified interface to control the number of threads used for parallel computations throughout qualpal. It supports both OpenMP and non-OpenMP builds:

- When OpenMP is available, it sets and queries the OpenMP thread count.
- When OpenMP is not available, all computations run sequentially (single-threaded).

By default, the thread count is set to half the number of available hardware threads (typically the number of physical CPU cores), but can be changed at runtime.

<a id="documentation-note"></a>

### Note

This class is not thread-safe. Set the thread count before starting parallel work.

 

```cpp
qualpal::Threads::set(4);  // Set to use 4 threads
std::size_t n = qualpal::Threads::get();  // Get current thread count
```

<a id="cpp-methods"></a>

## Methods

<a id="cpp-6d4bef812d722ca0e3aaa506fd1264f2256f1c6e3f613c6f0ec6738559375101"></a>

### `get`

```cpp
static inline std::size_t get()
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/threads.h>)

Get the current number of threads used for parallel computations. 

<a id="cpp-6d4bef812d722ca0e3aaa506fd1264f2256f1c6e3f613c6f0ec6738559375101-documentation-returns"></a>

##### Returns

The current thread count as set by [set()](<00c82e3786a929271b2b4c19118c342df1627c8d3a932179e801db820fbafdea#cpp-738beaff3d2f118a59e1873bc32552e62a165423d6949fd6135831c95a76d0cd>), or the default value. 

<a id="cpp-738beaff3d2f118a59e1873bc32552e62a165423d6949fd6135831c95a76d0cd"></a>

### `set`

```cpp
static inline void set(const std::size_t n)
```

[Source](<https://github.com/jolars/qualpal/blob/887b793a3795d298a62e49356a72314ce59dab0d/include/qualpal/threads.h>)

Set the number of threads to use for parallel computations. 

<a id="cpp-738beaff3d2f118a59e1873bc32552e62a165423d6949fd6135831c95a76d0cd-documentation-parameters"></a>

##### Parameters

**`n`**

Number of threads. Must be positive. 

 

<a id="cpp-738beaff3d2f118a59e1873bc32552e62a165423d6949fd6135831c95a76d0cd-documentation-throws"></a>

##### Throws

**`std::invalid_argument`**

if n is zero. 

 

<a id="cpp-738beaff3d2f118a59e1873bc32552e62a165423d6949fd6135831c95a76d0cd-documentation-note"></a>

##### Note

When OpenMP is enabled, this sets the OpenMP thread count globally.
