Skip to content

Make UniqueTimestampFactory thread-safe using lock-free atomic operations#71

Open
konard wants to merge 4 commits into
mainfrom
issue-41-217ce4e9
Open

Make UniqueTimestampFactory thread-safe using lock-free atomic operations#71
konard wants to merge 4 commits into
mainfrom
issue-41-217ce4e9

Conversation

@konard

@konard konard commented Sep 13, 2025

Copy link
Copy Markdown
Member

Summary

  • Fixed race condition in UniqueTimestampFactory.Create() method that could produce duplicate timestamps in multi-threaded environments
  • Implemented thread-safe solution using Interlocked.CompareExchange with retry loop for optimal performance
  • Added comprehensive test suite to validate thread-safety and timestamp uniqueness

Technical Details

The original implementation had a race condition at UniqueTimestampFactory.cs:23:

_lastTicks = utcTicks > _lastTicks ? utcTicks : _lastTicks + 1;

Multiple threads could read the same _lastTicks value simultaneously, leading to duplicate timestamps.

The solution uses lock-free atomic operations:

ulong lastTicks, newTicks;
do
{
    lastTicks = _lastTicks;
    newTicks = utcTicks > lastTicks ? utcTicks : lastTicks + 1;
}
while (Interlocked.CompareExchange(ref _lastTicks, newTicks, lastTicks) != lastTicks);

Test Coverage

Added 3 new comprehensive tests:

  • ThreadSafetyTest: Validates uniqueness across 5,000 concurrent timestamps (50 tasks × 100 timestamps)
  • SequentialOrderTest: Ensures timestamps maintain ascending order in single-threaded execution
  • ConcurrentSequentialOrderTest: Verifies ordering within concurrent execution scenarios

All tests pass, confirming the thread-safety implementation works correctly.

Performance Impact

  • Lock-free implementation provides better performance than mutex-based approaches
  • Uses CPU-level atomic operations for minimal overhead
  • Maintains the [MethodImpl(MethodImplOptions.AggressiveInlining)] optimization

🤖 Generated with Claude Code


Resolves #41

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #41
@konard konard self-assigned this Sep 13, 2025
…ions

Fixes the race condition in Create() method where multiple threads could read
the same _lastTicks value simultaneously, leading to duplicate timestamps.

Uses Interlocked.CompareExchange in a retry loop to atomically update _lastTicks,
ensuring thread-safety without locks for optimal performance.

Added comprehensive tests:
- ThreadSafetyTest: Validates uniqueness across 5000 concurrent timestamps
- SequentialOrderTest: Ensures timestamps maintain ascending order
- ConcurrentSequentialOrderTest: Verifies ordering within concurrent execution

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Make a thread-safe version of the factory Make UniqueTimestampFactory thread-safe using lock-free atomic operations Sep 13, 2025
@konard konard marked this pull request as ready for review September 13, 2025 17:04
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>'
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.

Make a thread-safe version of the factory

1 participant