mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
Add thead safety to terrain data (#1999)
Allow safely reading terrain data from other threads
This commit is contained in:
parent
d6220ff779
commit
aeca6a8f53
3 changed files with 101 additions and 25 deletions
|
|
@ -2,6 +2,7 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace MinecraftClient.Mapping
|
||||
{
|
||||
|
|
@ -17,6 +18,11 @@ namespace MinecraftClient.Mapping
|
|||
/// </summary>
|
||||
private readonly Chunk[] chunks = new Chunk[ColumnSize];
|
||||
|
||||
/// <summary>
|
||||
/// Lock for thread safety
|
||||
/// </summary>
|
||||
private readonly ReaderWriterLockSlim chunkLock = new ReaderWriterLockSlim();
|
||||
|
||||
/// <summary>
|
||||
/// Get or set the specified chunk column
|
||||
/// </summary>
|
||||
|
|
@ -27,11 +33,27 @@ namespace MinecraftClient.Mapping
|
|||
{
|
||||
get
|
||||
{
|
||||
return chunks[chunkY];
|
||||
chunkLock.EnterReadLock();
|
||||
try
|
||||
{
|
||||
return chunks[chunkY];
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunkLock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
chunks[chunkY] = value;
|
||||
chunkLock.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
chunks[chunkY] = value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
chunkLock.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue