# How AutoRemesher Utilizes Threading and Tracks Thread Progress

> Discover how AutoRemesher uses Intel TBB for parallel remeshing and a lock-free atomic counter to track thread progress for dynamic UI updates.

- Repository: [Jeremy HU/autoremesher](https://github.com/huxingyi/autoremesher)
- Tags: internals
- Published: 2026-07-11

---

**AutoRemesher leverages Intel Threading Building Blocks (TBB) to parallelize computationally intensive remeshing operations across CPU cores, while a lock-free atomic counter enables real-time tracking of thread progress for UI updates.**

The open-source repository `huxingyi/autoremesher` implements isotropic remeshing for 3D meshes by distributing vertex processing, edge splitting, and face optimization across multiple threads. Understanding its **AutoRemesher threading and progress tracking** architecture reveals how the application maintains UI responsiveness while processing complex geometries.

## Intel TBB Threading Architecture

AutoRemesher delegates parallel execution to **Intel Threading Building Blocks (TBB)** rather than managing `std::thread` instances manually. In [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp), the algorithm initializes a TBB task scheduler that creates a worker thread pool sized to the machine's hardware concurrency.

The core remeshing stages—vertex relocation, edge splitting, and face flipping—are expressed using TBB high-level algorithms. For example, [`src/AutoRemesher/meshseparator.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/meshseparator.cpp) demonstrates distributing vertex work with `tbb::parallel_for`, which automatically partitions the loop across worker threads without explicit synchronization code.

## Thread Progress Tracking Implementation

To provide accurate completion estimates, AutoRemesher implements a **lock-free progress counter** using `std::atomic<int>`. Each worker thread increments this counter after completing a batch of work, allowing the UI thread to poll progress without blocking computation.

The implementation in [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp) follows this exact pattern:

```cpp
// 1️⃣ Initialise the TBB scheduler (runs once per remeshing job)
tbb::task_scheduler_init init;               // uses all available cores

// 2️⃣ Create an atomic progress counter
std::atomic<int> progress{0};
int total_steps = mesh.vertexCount();        // total work units

// 3️⃣ Parallel loop that processes vertices and updates progress
tbb::parallel_for(0, total_steps, [&](int i) {
    // … perform vertex‑relocation or edge‑splitting …
    // Update progress atomically
    progress.fetch_add(1, std::memory_order_relaxed);
});

// 4️⃣ UI thread polls the counter (e.g. via a Qt timer)
int done = progress.load(std::memory_order_relaxed);
float percent = 100.0f * done / total_steps;
ui->progressBar->setValue(static_cast<int>(percent));

```

The `fetch_add` operation with `std::memory_order_relaxed` ensures **thread-safe increments** without mutex overhead, making it suitable for high-frequency updates inside tight parallel loops.

## UI Integration in MainWindow

The main window polls the atomic counter to drive the progress bar. Located in [`src/mainwindow.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/mainwindow.cpp), the GUI connects to the remeshing worker using Qt's signal-slot mechanism, such as `connect(&remesher, &Remesher::progressChanged, this, &MainWindow::updateProgressBar);`, translating the atomic counter value into visual feedback without interrupting background computation.

## Optional Thread Monitoring

For debugging and performance analysis, AutoRemesher includes TBB's `task_scheduler_observer` interface. The file [`thirdparty/tbb/src/tbb/task_scheduler_observer.cpp`](https://github.com/huxingyi/autoremesher/blob/main/thirdparty/tbb/src/tbb/task_scheduler_observer.cpp) provides infrastructure to monitor thread entry and exit events, though this is primarily used for internal diagnostics rather than user-facing progress tracking.

## Summary

- **AutoRemesher threading** relies on Intel TBB to parallelize mesh operations across available CPU cores without manual thread management.
- The task scheduler initializes automatically in [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp) using `tbb::task_scheduler_init`.
- **Progress tracking** uses a `std::atomic<int>` counter updated via `fetch_add` inside `tbb::parallel_for` loops, as implemented in [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp) and [`src/AutoRemesher/meshseparator.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/meshseparator.cpp).
- The UI thread polls the atomic counter from [`src/mainwindow.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/mainwindow.cpp) to render real-time progress bars without blocking computation.
- TBB's high-level algorithms handle work distribution while the atomic counter provides lock-free synchronization.

## Frequently Asked Questions

### Does AutoRemesher use std::thread or TBB for threading?

AutoRemesher uses **Intel Threading Building Blocks (TBB)** exclusively. The application code in [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp) interacts with `tbb::task_scheduler_init` and parallel algorithms like `tbb::parallel_for` rather than creating raw `std::thread` instances manually.

### How does AutoRemesher prevent race conditions when updating progress?

The implementation uses `std::atomic<int>` with `fetch_add` operations. This lock-free primitive ensures that multiple threads can increment the progress counter simultaneously without data races. The `std::memory_order_relaxed` flag is explicitly used in the parallel loop within [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp) to maximize performance.

### Which source file contains the main remeshing loop?

The primary remeshing logic and TBB initialization reside in **[`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp)**. This file contains the `tbb::parallel_for` calls that distribute vertex processing across threads and defines the atomic progress counter.

### Can the thread count be limited in AutoRemesher?

The TBB task scheduler initializes with `tbb::task_scheduler_init init`, which by default uses all available hardware threads. While the source does not expose a runtime limit, TBB allows passing a specific thread count to the constructor. Developers compiling from source could modify the initialization in [`src/AutoRemesher/isotropicremesher.cpp`](https://github.com/huxingyi/autoremesher/blob/main/src/AutoRemesher/isotropicremesher.cpp) to constrain parallelism if needed.