Skip to content

Use compare_exchange_weak in steal loop for potential performance imp…#3285

Open
xin-ok wants to merge 1 commit intoapache:masterfrom
xin-ok:optimize/change_cas_weak
Open

Use compare_exchange_weak in steal loop for potential performance imp…#3285
xin-ok wants to merge 1 commit intoapache:masterfrom
xin-ok:optimize/change_cas_weak

Conversation

@xin-ok
Copy link
Copy Markdown

@xin-ok xin-ok commented May 4, 2026

What problem does this PR solve?

Issue Number:

Problem Summary:
在 WorkStealingQueue 的 steal 函数中,使用 compare_exchange_strong 在循环中重试。
由于已经在 do-while 循环中,改用 compare_exchange_weak 可以获得更好的性能,
同时保持语义正确

What is changed and the side effects?

Changed:

  • _top.compare_exchange_strong 改为 _top.compare_exchange_weak
  • 保持其他逻辑不变

Side effects:

  • Performance effects:
  • Breaking backward compatibility:

Check List:

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR makes a small performance-oriented tweak to bthread::WorkStealingQueue::steal() by switching the CAS operation in the retry loop from compare_exchange_strong to compare_exchange_weak, which is safe in a loop and may reduce overhead on some architectures while preserving correctness.

Changes:

  • Replace _top.compare_exchange_strong(...) with _top.compare_exchange_weak(...) inside the steal() do/while retry loop.
  • Keep memory orders and the rest of the steal logic unchanged.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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