Skip to content

fix: harden multithreading synchronization and v2 docs - #31

Merged
nihiL7331 merged 1 commit into
nihiL7331:mainfrom
csheldrick:main
Aug 8, 2026
Merged

fix: harden multithreading synchronization and v2 docs#31
nihiL7331 merged 1 commit into
nihiL7331:mainfrom
csheldrick:main

Conversation

@csheldrick

Copy link
Copy Markdown
Contributor

Follow-up to #30 focused on threading correctness, portability, and a few v2 regressions found after the multithreading rewrite.

What changed

  • Restore opt-in stb-style POLESITTER_IMPLEMENTATION semantics.
  • Synchronize thread-pool queue/state access under the existing spinlock instead of relying on volatile.
  • Avoid requiring AVX intrinsics just to issue an x86 spin-wait hint.
  • Fix the ST-vs-MT equivalence test so force mismatches actually fail the test.
  • Update the README quickstart to match the v2 API, including:
    • PS_MULTITHREADING
    • max_particles
    • configurable theta
    • thrd_cnt
    • ps_destroy
    • removal of the now-internal arena reset call

Why

The multithreading refactor introduced a few cross-cutting regressions while changing the execution architecture.

In particular, the thread pool was reading cnt, active_jobs, and shutdown_flag concurrently outside the spinlock and relying on volatile. In C, volatile does not provide atomicity or inter-thread synchronization, so those accesses constitute a data race.

The updated implementation keeps the existing spinlock architecture but ensures shared pool state is inspected and modified while holding the lock.

The ST-vs-MT comparison test also printed force mismatches without incrementing its mismatch counter, allowing that check to report success despite differing forces.

Finally, the v2 README example still reflected the previous API and would not correctly initialize the new radix/threading state.

Validation

The final branch passes:

  • clang-format
  • clang-tidy
  • Ubuntu build/tests
  • macOS/ARM build/tests
  • Windows build/tests
  • threaded ST-vs-MT equivalence validation
  • existing direct-reference numerical accuracy tests

This intentionally leaves the overall threading architecture unchanged and limits the patch to correctness, portability, and documentation fixes.

Restore opt-in single-header implementation semantics, synchronize shared thread-pool state under the existing spinlock, make x86 spin waiting portable without AVX, fix the ST/MT force comparator, and update the README for the v2 API.
@nihiL7331

Copy link
Copy Markdown
Owner

Looks good. Merging now.

@nihiL7331
nihiL7331 merged commit 10794c2 into nihiL7331:main Aug 8, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants