© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•14mo ago•
38 replies
hugeman

Race condition with Interlocked.Exchange

Hi, I found a weird bug which I can't quite understand...

This is my class: https://github.com/AngryCarrot789/FramePFX/blob/master/FramePFX/Utils/RDA/RateLimitedDispatchAction.cs, it lets me add forced minimum interval between sequential executions (e.g. say you're dragging around a UI control but you only want to update the position text every say 0.1 seconds)

InvokeAsync is called by the user and it choses whether to create a new task or set the current task to a "continue" state. ExecuteCore actually executes the action.

At the very bottom is a generic parameter version, and at one point I hit a break point on the Debug.Assert(false) statement, which to me should be impossible, because
ExecuteCore
ExecuteCore
can only be executed if
InvokeAsync
InvokeAsync
has been invoked. And even if
InvokeAsync
InvokeAsync
gets invoked 10000 times before
ExecuteCore
ExecuteCore
, the
Interlocked.Exchange
Interlocked.Exchange
should ensure that
currentValue
currentValue
is swapped atomically... right? I rarely do multi threaded code so please tell me if i'm being turbo dumb and misusing Interlocked 😄
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Possible race condition?
C#CC# / help
16mo ago
Mixing `Volatile` with `Interlocked`
C#CC# / help
10mo ago
Threadsafety and Interlocked.CompareExchange
C#CC# / help
2y ago
if statements with more than once condition
C#CC# / help
2y ago