Skip to content

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

Merged
chenBright merged 1 commit intoapache:masterfrom
xin-ok:optimize/change_cas_weak
May 6, 2026
Merged

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

Conversation

@xin-ok
Copy link
Copy Markdown
Contributor

@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.

@wwbmmm
Copy link
Copy Markdown
Contributor

wwbmmm commented May 6, 2026

LGTM

Copy link
Copy Markdown
Contributor

@chenBright chenBright left a comment

Choose a reason for hiding this comment

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

LGTM

@chenBright chenBright merged commit 2a02d9e into apache:master May 6, 2026
21 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.

4 participants