Skip to content

Harden thread/async robustness in optimize and formatters#383

Open
HamzaYslmn wants to merge 1 commit into
fontanf:masterfrom
HamzaYslmn:pr/thread-async-robustness
Open

Harden thread/async robustness in optimize and formatters#383
HamzaYslmn wants to merge 1 commit into
fontanf:masterfrom
HamzaYslmn:pr/thread-async-robustness

Conversation

@HamzaYslmn

Copy link
Copy Markdown
Contributor

Hardens thread and async handling across the optimize entry points and algorithm formatters (box, boxstacks, irregular, onedimensional, rectangle, rectangleguillotine) to avoid races and lost results under concurrency.


// Take a snapshot of the output so the (potentially heavy, file-I/O
// performing) callback can run outside the critical section.
output_snapshot = output_;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copy is not ideal. The output structure might be large. It will slow down the solver for instances where many solutions are found. The solution I see is to handle I/O issues in the callback directly. Let me know if you see a better alternative

- Move new_solution_callback (file I/O) out of the formatter mutex critical
  section by snapshotting the output under the lock and invoking the callback
  after unlocking, so worker threads no longer serialize on the callback's
  disk writes.
- Convert manual mutex_.lock()/unlock() in all update_* methods to RAII
  std::unique_lock for exception safety (no deadlock if a callback throws).
- Add a ThreadJoinGuard RAII helper and place it after every worker-thread
  vector so threads are joined during stack unwinding instead of triggering
  std::terminate() if an exception is thrown before the explicit join loop.
@fontanf fontanf force-pushed the pr/thread-async-robustness branch 2 times, most recently from a697525 to c9218ea Compare June 14, 2026 13:48
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.

2 participants