ThreadTask: fix thread lifetime ordering and make detach flag atomic by rajanyadav0307 · Pull Request #3662 · aws/aws-sdk-cpp

Issue #, if available:
N/A

Description of changes:
This PR improves the thread-safety and lifetime correctness of ThreadTask.

Changes include:

  • Reordering data members so std::thread is destroyed last, ensuring all
    members accessed by the worker thread remain valid during destruction.
  • Promoting m_detached to std::atomic<bool> to prevent data races when
    accessed concurrently by multiple threads.
  • Updating the constructor initializer list to match the member declaration
    order and using a lambda for thread startup for improved clarity.

These changes do not modify the public API and are purely internal
correctness and safety improvements.

Check all that applies:

  • Did a review by yourself.
  • Added proper tests to cover this PR. (Existing threading behavior is exercised; no new tests required.)
  • Checked if this PR is a breaking (APIs have been changed) change.
  • Checked if this PR will not introduce cross-platform inconsistent behavior.
  • Checked if this PR would require a ReadMe/Wiki update.

Check which platforms you have built SDK on to verify the correctness of this PR.

  • Linux
  • Windows
  • Android
  • MacOS
  • IOS
  • Other Platforms

By submitting this pull request, I confirm that you can use, modify, copy, and
redistribute this contribution, under the terms of your choice.