# Thread

<a id="thread-1"></a>

# Thread

```cpp
#include <icy/thread.h>

class Thread
```

[Defined in src/base/include/icy/thread.h:32](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L32)

> **Inherits:** [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner)

Platform-independent wrapper around an operating system thread.

This class inherits the [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) interface and may be used with any implementation that's powered by an asynchronous [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner).

## List of all members

| Name | Kind | Owner |
|------|------|-------|
| [`Thread`](#thread-2) | `function` | Declared here |
| [`Thread`](#thread-3) | `function` | Declared here |
| [`~Thread`](#thread-4) | `function` | Declared here |
| [`start`](#start-8) | `function` | Declared here |
| [`start`](#start-9) | `function` | Declared here |
| [`join`](#join) | `function` | Declared here |
| [`id`](#id-3) | `function` | Declared here |
| [`mainID`](#mainid) | `variable` | Declared here |
| [`currentID`](#currentid) | `function` | Declared here |
| [`_thread`](#_thread-3) | `variable` | Declared here |
| [`Thread`](#thread-5) | `function` | Declared here |
| [`operator=`](#operator-100) | `function` | Declared here |
| [`Thread`](#thread-6) | `function` | Declared here |
| [`operator=`](#operator-101) | `function` | Declared here |
| [`async`](#async-3) | `function` | Declared here |
| [`Ptr`](#ptr-4) | `typedef` | Declared here |
| [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-1) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`~Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-2) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`start`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#start-3) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`running`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#running) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`cancel`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#cancel-2) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`cancelled`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#cancelled-1) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`repeating`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#repeating) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`setRepeating`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#setrepeating) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`async`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#async-1) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`tid`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#tid) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`waitForExit`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#waitforexit) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`_context`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#_context) | `variable` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-3) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`operator=`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#operator-81) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-4) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |
| [`operator=`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#operator-82) | `function` | Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) |

## Inherited from [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner)

| Kind | Name | Description |
|------|------|-------------|
| `function` | [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-1)  |  |
| `function` | [`~Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-2) `virtual` |  |
| `function` | [`start`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#start-3) `virtual` | Starts the asynchronous context with the given callback. The callback must remain valid for the lifetime of the [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner). |
| `function` | [`running`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#running) `const` | Returns true if the async context is currently running. |
| `function` | [`cancel`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#cancel-2)  | Signals the async context to stop at the earliest opportunity. |
| `function` | [`cancelled`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#cancelled-1) `const` | Returns true if the context has been cancelled. The implementation is responsible for exiting as soon as possible after cancellation. |
| `function` | [`repeating`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#repeating) `const` | Returns true if the runner is in repeating mode. |
| `function` | [`setRepeating`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#setrepeating)  | Enables or disables repeating mode. When repeating, the target function is invoked repeatedly until [`cancel()`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#cancel-2) is called. This normalizes behaviour across thread-based and event-loop-based [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner) implementations. Must be called before [`start()`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#start-3). |
| `function` | [`async`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#async-1) `virtual` `const` | Returns true if the implementation is thread-based. |
| `function` | [`tid`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#tid) `const` | Returns the native thread ID of the thread running the async context. |
| `function` | [`waitForExit`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#waitforexit)  | Blocks until the async context exits or the timeout elapses. The context should be cancelled before calling this method. Must be called from outside the runner's own thread to avoid deadlock. |
| `variable` | [`_context`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#_context)  | Shared pointer to the internal [Context](https://auscaster.com/icey/docs/api-reference/icy-Runner-Context.md#context). |
| `function` | [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-3)  | NonCopyable and NonMovable. |
| `function` | [`operator=`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#operator-81)  | Deleted assignment operator. |
| `function` | [`Runner`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#runner-4)  | Deleted constructor. |
| `function` | [`operator=`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#operator-82)  | Deleted assignment operator. |

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Thread`](#thread-2)  | Default constructor. |
|  | [`Thread`](#thread-3) `inline` `explicit` | Constructs a [`Thread`](#thread-1) and immediately starts it with the given function and arguments. |
|  | [`~Thread`](#thread-4) `virtual` | Destructor. |
| `void` | [`start`](#start-8) `inline` | Starts the thread with a variadic function and arguments. The thread is started immediately; the previous thread must have exited before calling again. |
| `void` | [`start`](#start-9) `virtual` `override` | Starts the thread with a `std::function` callback. Overrides [`Runner::start`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#start-3); delegates to the variadic `start` template. |
| `void` | [`join`](#join)  | Wait until the thread exits. |
| `std::thread::id` | [`id`](#id-3) `const` | Return the native thread handle. |

---

<a id="thread-2"></a>

### Thread

```cpp
Thread()
```

[Defined in src/base/include/icy/thread.h:38](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L38)

Default constructor.

---

<a id="thread-3"></a>

### Thread

`inline` `explicit`

```cpp
template<typename Function, typename... Args> inline explicit Thread(Function && func, Args &&... args)
```

[Defined in src/base/include/icy/thread.h:46](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L46)

Constructs a [`Thread`](#thread-1) and immediately starts it with the given function and arguments.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `func` | `Function &&` | Callable to execute on the new thread. |
| `args` | `Args &&...` | Arguments forwarded to `func`. |

---

<a id="thread-4"></a>

### ~Thread

`virtual`

```cpp
virtual ~Thread()
```

[Defined in src/base/include/icy/thread.h:54](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L54)

Destructor.

---

<a id="start-8"></a>

### start

`inline`

```cpp
template<typename Function, typename... Args> inline void start(Function && func, Args &&... args)
```

[Defined in src/base/include/icy/thread.h:63](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L63)

Starts the thread with a variadic function and arguments. The thread is started immediately; the previous thread must have exited before calling again.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `func` | `Function &&` | Callable to execute on the new thread. |
| `args` | `Args &&...` | Arguments forwarded to `func`. |

---

<a id="start-9"></a>

### start

`virtual` `override`

```cpp
virtual void start(std::function< void()> func) override
```

[Defined in src/base/include/icy/thread.h:78](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L78)

Starts the thread with a `std::function` callback. Overrides [`Runner::start`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#start-3); delegates to the variadic `start` template.

#### Reimplements

- [`Runner::start`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#start-3)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `func` | `std::function< void()>` | Callable to execute on the new thread. |

---

<a id="join"></a>

### join

```cpp
void join()
```

[Defined in src/base/include/icy/thread.h:81](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L81)

Wait until the thread exits.

---

<a id="id-3"></a>

### id

`const`

```cpp
std::thread::id id() const
```

[Defined in src/base/include/icy/thread.h:84](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L84)

Return the native thread handle.

## Public Static Attributes

| Return | Name | Description |
|--------|------|-------------|
| `const std::thread::id` | [`mainID`](#mainid) `static` | Accessor for the main thread ID. |

---

<a id="mainid"></a>

### mainID

`static`

```cpp
const std::thread::id mainID
```

[Defined in src/base/include/icy/thread.h:90](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L90)

Accessor for the main thread ID.

## Public Static Methods

| Return | Name | Description |
|--------|------|-------------|
| `std::thread::id` | [`currentID`](#currentid) `static` | Return the native thread ID of the current thread. |

---

<a id="currentid"></a>

### currentID

`static`

```cpp
static std::thread::id currentID()
```

[Defined in src/base/include/icy/thread.h:87](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L87)

Return the native thread ID of the current thread.

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| `std::thread` | [`_thread`](#_thread-3)  |  |

---

<a id="_thread-3"></a>

### _thread

```cpp
std::thread _thread
```

[Defined in src/base/include/icy/thread.h:101](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L101)

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Thread`](#thread-5)  | NonCopyable and NonMovable. |
| [`Thread`](#thread-1) & | [`operator=`](#operator-100)  | Deleted assignment operator. |
|  | [`Thread`](#thread-6)  | Deleted constructor. |
| [`Thread`](#thread-1) & | [`operator=`](#operator-101)  | Deleted assignment operator. |
| `bool` | [`async`](#async-3) `virtual` `const` `override` | Returns true if the implementation is thread-based. |

---

<a id="thread-5"></a>

### Thread

```cpp
Thread(const Thread &) = delete
```

[Defined in src/base/include/icy/thread.h:94](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L94)

NonCopyable and NonMovable.

---

<a id="operator-100"></a>

### operator=

```cpp
Thread & operator=(const Thread &) = delete
```

[Defined in src/base/include/icy/thread.h:95](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L95)

Deleted assignment operator.

---

<a id="thread-6"></a>

### Thread

```cpp
Thread(Thread &&) = delete
```

[Defined in src/base/include/icy/thread.h:96](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L96)

Deleted constructor.

---

<a id="operator-101"></a>

### operator=

```cpp
Thread & operator=(Thread &&) = delete
```

[Defined in src/base/include/icy/thread.h:97](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L97)

Deleted assignment operator.

---

<a id="async-3"></a>

### async

`virtual` `const` `override`

```cpp
virtual bool async() const override
```

[Defined in src/base/include/icy/thread.h:99](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L99)

Returns true if the implementation is thread-based. 
#### Returns
True for thread-backed runners, false for event-loop-driven runners.

#### Reimplements

- [`Runner::async`](https://auscaster.com/icey/docs/api-reference/icy-Runner.md#async-1)

## Public Types

| Name | Description |
|------|-------------|
| [`Ptr`](#ptr-4)  |  |

---

<a id="ptr-4"></a>

### Ptr

```cpp
using Ptr = std::shared_ptr< Thread >
```

Type: std::shared_ptr< [`Thread`](#thread-1) >

[Defined in src/base/include/icy/thread.h:35](https://github.com/auscaster/icey/blob/main/src/base/include/icy/thread.h#L35)
