-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix thread-safety problem in World.WordSize by applying Interlocked o…
…perations
- Loading branch information
Showing
2 changed files
with
48 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Arch.Core; | ||
using static NUnit.Framework.Assert; | ||
|
||
namespace Arch.Tests; | ||
|
||
[TestFixture] | ||
public class MultiThreadTest | ||
{ | ||
/// <summary> | ||
/// Checks if the <see cref="World.WorldSize"/> is correct when creating World multithreaded. | ||
/// </summary> | ||
[Test] | ||
public void MultiThreadedCreateAndDestroy() | ||
{ | ||
int originalWorldSize = World.WorldSize; | ||
const int testCount = 10; | ||
for (int i = 0; i < testCount; ++i) | ||
{ | ||
var threads = new List<Thread>(); | ||
for (var j = 0; j < Environment.ProcessorCount; j++) | ||
{ | ||
var thread = new Thread(() => | ||
{ | ||
for (var j = 0; j < 1000; j++) | ||
{ | ||
var world = World.Create(); | ||
World.Destroy(world); | ||
} | ||
}); | ||
threads.Add(thread); | ||
} | ||
|
||
threads.ForEach(t => t.Start()); | ||
|
||
foreach (var thread in threads) | ||
{ | ||
thread.Join(); | ||
} | ||
|
||
That(World.WorldSize, Is.EqualTo(originalWorldSize)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters