【发布时间】:2017-04-14 13:58:41
【问题描述】:
在下面的代码中,我锁定了 guid,以尝试使其线程安全。 在我的示例应用程序中,我大约每运行 10 次就会得到一个“重复密钥”。 Aka,我得到了一个副本,这不是我需要的。
有没有办法让“.NextGuid”线程安全?
using System;
namespace MyConsoleOne.BAL
{
public class GuidStore
{
private static object objectlock = new object();
private Guid StartingGuid { get; set; }
private Guid? LastGuidHolder { get; set; }
public GuidStore(Guid startingGuid)
{
this.StartingGuid = startingGuid;
}
public Guid? GetNextGuid()
{
lock (objectlock)
{
if (this.LastGuidHolder.HasValue)
{
this.LastGuidHolder = Increment(this.LastGuidHolder.Value);
}
else
{
this.LastGuidHolder = Increment(this.StartingGuid);
}
}
return this.LastGuidHolder;
}
private Guid Increment(Guid guid)
{
byte[] bytes = guid.ToByteArray();
byte[] order = { 15, 14, 13, 12, 11, 10, 9, 8, 6, 7, 4, 5, 0, 1, 2, 3 };
for (int i = 0; i < 16; i++)
{
if (bytes[order[i]] == byte.MaxValue)
{
bytes[order[i]] = 0;
}
else
{
bytes[order[i]]++;
return new Guid(bytes);
}
}
throw new OverflowException("Guid.Increment failed.");
}
}
}
using MyConsoleOne.BAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyConsoleOne
{
class Program
{
static void Main(string[] args)
{
GuidStore gs = new GuidStore(Guid.NewGuid());
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(i);
Dictionary<Guid, int> guids = new Dictionary<Guid, int>();
Parallel.For(0, 1000, j =>
{
Guid? currentGuid = gs.GetNextGuid();
guids.Add(currentGuid.Value, j);
Console.WriteLine(currentGuid);
}); // Parallel.For
}
Console.WriteLine("Press ENTER to Exit");
Console.ReadLine();
}
}
}
我的代码是:
由于我收到“为什么不使用 Guid.NewGuid”的问题,我将在此处提供原因:
我有一个父进程,它有一个由 Guid.NewGuid() 创建的唯一标识符。我将其称为“父 guid”。该父进程将创建 N 个文件。如果我从头开始编写,我只会在文件名的末尾附加“N”。因此,例如,如果父 Guid 是“11111111-1111-1111-1111-111111111111”,我会写文件
"11111111-1111-1111-1111-111111111111_1.txt"
"11111111-1111-1111-1111-111111111111_2.txt"
"11111111-1111-1111-1111-111111111111_3.txt"
等等。但是,通过与客户端的现有“合同” ::: 文件名必须有一个(唯一的)Guid,并且没有那个“N”(1,2,等等,等等)值在文件名中(这个“合同”已经存在多年,所以它几乎是一成不变的)。使用此处列出的功能,我将能够保留“合同”,但文件名松散地绑定到“父”Guid(父级由 Guid.NewGuid() 生成)。冲突不是文件名的问题(它们被放在一个不同的文件夹下以执行“进程”)。冲突是“父”Guid 的问题。但同样,这已经被 Guid.NewGuid 处理了。
因此,使用“11111111-1111-1111-1111-111111111111”的起始 Guid,我将能够编写如下文件名:
OTHERSTUFF_111111111-1111-1111-1111-111111111112_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111113_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111114_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111115_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111116_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111117_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111118_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-111111111119_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-11111111111a_MORESTUFF.txt
OTHERSTUFF_111111111-1111-1111-1111-11111111111b_MORESTUFF.txt
因此,在我上面的示例中,“父 guid”由“this.StartingGuid”表示....然后我得到“递增”的 guid。
还有。我可以编写更好的单元测试,因为现在我会提前知道文件名。
追加:
最终代码版本:
public class GuidStore
{
private static object objectlock = new object();
private static int[] byteOrder = { 15, 14, 13, 12, 11, 10, 9, 8, 6, 7, 4, 5, 0, 1, 2, 3 };
private Guid StartingGuid { get; set; }
private Guid? LastGuidHolder { get; set; }
public GuidStore(Guid startingGuid)
{
this.StartingGuid = startingGuid;
}
public Guid GetNextGuid()
{
return this.GetNextGuid(0);
}
public Guid GetNextGuid(int firstGuidOffSet)
{
lock (objectlock)
{
if (this.LastGuidHolder.HasValue)
{
this.LastGuidHolder = Increment(this.LastGuidHolder.Value);
}
else
{
this.LastGuidHolder = Increment(this.StartingGuid);
for (int i = 0; i < firstGuidOffSet; i++)
{
this.LastGuidHolder = Increment(this.LastGuidHolder.Value);
}
}
return this.LastGuidHolder.Value;
}
}
private static Guid Increment(Guid guid)
{
var bytes = guid.ToByteArray();
var canIncrement = byteOrder.Any(i => ++bytes[i] != 0);
return new Guid(canIncrement ? bytes : new byte[16]);
}
}
和单元测试:
public class GuidStoreUnitTests
{
[TestMethod]
public void GetNextGuidSimpleTest()
{
Guid startingGuid = new Guid("11111111-1111-1111-1111-111111111111");
GuidStore gs = new GuidStore(startingGuid);
List<Guid> guids = new List<Guid>();
const int GuidCount = 10;
for (int i = 0; i < GuidCount; i++)
{
guids.Add(gs.GetNextGuid());
}
Assert.IsNotNull(guids);
Assert.AreEqual(GuidCount, guids.Count);
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111112")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111113")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111114")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111115")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111116")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111117")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111118")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-111111111119")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-11111111111a")));
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-11111111111b")));
}
[TestMethod]
public void GetNextGuidWithOffsetSimpleTest()
{
Guid startingGuid = new Guid("11111111-1111-1111-1111-111111111111");
GuidStore gs = new GuidStore(startingGuid);
List<Guid> guids = new List<Guid>();
const int OffSet = 10;
guids.Add(gs.GetNextGuid(OffSet));
Assert.IsNotNull(guids);
Assert.AreEqual(1, guids.Count);
Assert.IsNotNull(guids.FirstOrDefault(g => g == new Guid("11111111-1111-1111-1111-11111111111c")));
}
[TestMethod]
public void GetNextGuidMaxRolloverTest()
{
Guid startingGuid = new Guid("ffffffff-ffff-ffff-ffff-ffffffffffff");
GuidStore gs = new GuidStore(startingGuid);
List<Guid> guids = new List<Guid>();
const int OffSet = 10;
guids.Add(gs.GetNextGuid(OffSet));
Assert.IsNotNull(guids);
Assert.AreEqual(1, guids.Count);
Assert.IsNotNull(guids.FirstOrDefault(g => g == Guid.Empty));
}
[TestMethod]
public void GetNextGuidThreadSafeTest()
{
Guid startingGuid = Guid.NewGuid();
GuidStore gs = new GuidStore(startingGuid);
/* The "key" of the ConcurrentDictionary must be unique, so this will catch any duplicates */
ConcurrentDictionary<Guid, int> guids = new ConcurrentDictionary<Guid, int>();
Parallel.For(
0,
1000,
j =>
{
Guid currentGuid = gs.GetNextGuid();
if (!guids.TryAdd(currentGuid, j))
{
throw new ArgumentOutOfRangeException("GuidStore.GetNextGuid ThreadSafe Test Failed");
}
}); // Parallel.For
}
[TestMethod]
public void GetNextGuidTwoRunsProduceSameResultsTest()
{
Guid startingGuid = Guid.NewGuid();
GuidStore gsOne = new GuidStore(startingGuid);
/* The "key" of the ConcurrentDictionary must be unique, so this will catch any duplicates */
ConcurrentDictionary<Guid, int> setOneGuids = new ConcurrentDictionary<Guid, int>();
Parallel.For(
0,
1000,
j =>
{
Guid currentGuid = gsOne.GetNextGuid();
if (!setOneGuids.TryAdd(currentGuid, j))
{
throw new ArgumentOutOfRangeException("GuidStore.GetNextGuid ThreadSafe Test Failed");
}
}); // Parallel.For
gsOne = null;
GuidStore gsTwo = new GuidStore(startingGuid);
/* The "key" of the ConcurrentDictionary must be unique, so this will catch any duplicates */
ConcurrentDictionary<Guid, int> setTwoGuids = new ConcurrentDictionary<Guid, int>();
Parallel.For(
0,
1000,
j =>
{
Guid currentGuid = gsTwo.GetNextGuid();
if (!setTwoGuids.TryAdd(currentGuid, j))
{
throw new ArgumentOutOfRangeException("GuidStore.GetNextGuid ThreadSafe Test Failed");
}
}); // Parallel.For
bool equal = setOneGuids.Select(g => g.Key).OrderBy(i => i).SequenceEqual(
setTwoGuids.Select(g => g.Key).OrderBy(i => i), new GuidComparer<Guid>());
Assert.IsTrue(equal);
}
}
internal class GuidComparer<Guid> : IEqualityComparer<Guid>
{
public bool Equals(Guid x, Guid y)
{
return x.Equals(y);
}
public int GetHashCode(Guid obj)
{
return 0;
}
}
【问题讨论】:
-
在递增时获得随机 guid 会不会更容易?鉴于您计划使用多个线程,与两个线程将 id 从 1 增加到 2 相比,两个线程生成相同随机 guid 的可能性较小。
-
return在lock之外是否有原因?看起来如果一个线程在返回之前被挂起并且下一个线程被允许进入临界区,后者会修改变量并返回相同的值。 -
你为什么不使用 Guid.NewGuid()?使用 .NET 提供的工具。
-
由于有人认为他们会推出自己的 GUID 算法(
public int GetTempGuid() { return Random.Next(10000, 100000); }- 这些“全球唯一标识符”在我们拥有 10K 条记录时碰巧与真实记录发生冲突,因此不得不解散数据库)在一张桌子上)请帮大家一个忙,不要自己动手。算法之所以复杂是有原因的。 en.wikipedia.org/wiki/Globally_unique_identifier。如果您需要顺序 GUID,请 PInvokeUuidCreateSequential。 -
我已经通过对原始问题的编辑阐述了我的理由。我很感激这种担忧。 uuid 不会进入数据库。他们 guids (here) 用于文件名。是的,我知道 UuidCreateSequential。 stackoverflow.com/questions/8477664/…
标签: c# .net multithreading guid