简体   繁体   English

是否有最佳实践来实现静态功能,该功能在同时访问时提供独特的价值

[英]Is there a best practice to implement static function that gives unique value on simultaneous access

A static function retUnique() returns a unique value. 静态函数retUnique()返回唯一值。 My question is that if there are many users who are using the same function at a given point of time, what happens? 我的问题是,如果有很多用户在给定的时间点使用相同的功能,会发生什么? Is there a best practice to make sure that each users accessing this static function simultaneously get a unique value and also do not face threading issues. 是否有最佳实践来确保每个访问此静态函数的用户都同时获得唯一的值,并且不会遇到线程问题。

Can one give me an example? 可以给我一个例子吗?

There is nothing special about static functions that make them more or less safe to use on multiple threads. 使多线程或多或少安全的静态函数没有什么特别的。 Instead, you need to examine the data which the function accesses and modifies, and make sure that it still respects the integrity of that data when called concurrently. 相反,您需要检查函数访问和修改的数据,并确保在并发调用时它仍然尊重该数据的完整性。

Here's an example that might be relevant to your question: 这是一个可能与您的问题有关的示例:

private static int myUniqueID = 1;
public static int GetFreshUniqueID()
{
    lock(someObject)
    {
        return myUniqueID++;
    }
}

The key in this case to keeping it thread-safe is locking the function in some synchronization context so that we don't run into a situation where two threads get the same ID before either of them get around to incrementing it. 在这种情况下,保持线程安全的关键是将函数锁定在某些同步上下文中,这样我们就不会遇到两个线程在每个线程都获得递增之前获得相同ID的情况。 Your situation may vary. 您的情况可能会有所不同。

Isn't this a simple synchronization problem? 这不是一个简单的同步问题吗?

Apply a lock at the beginning of the function and release it at end. 在函数的开头应用一个锁,并在结尾处释放它。

Assuming you want to simply return a unique incrementing integer value, the simplest safe approach is probably to use a private static counter and a private static lock object. 假设您只想返回唯一的递增整数值,最简单的安全方法可能是使用私有静态计数器和私有静态锁对象。 Something like: 就像是:

private static int s_UniqueCounter; // starts at 0 by default
private static readonly object s_UniqueCounterLock = new object();

public static int GetUnique()
{
    lock (s_UniqueCounterLock)
    {
        s_UniqueCounter++;
        return s_UniqueCounter;
    }
}

It's up to you to make sure the particular lock object is used to protect any other access to the static counter member (which is why they are declared private, of course, so the class which owns them controls any access). 您需要确保使用特定的锁对象来保护对静态计数器成员的任何其他访问(当然,这就是为什么将它们声明为私有的原因,因此拥有它们的类可以控制任何访问)。 It probably shouldn't be accessed anywhere else, for this use, but you might have something look at the current value to see how many times it has been called. 为此用途,可能不应该在其他任何地方访问它,但是您可能会对当前值有所了解,以查看它被调用了多少次。 That should probably be protected by the lock as well (to make sure the result is current): 那可能也应该受到锁的保护(以确保结果是最新的):

internal static int QueryUniqueCount() // or this could be private, or public
{
    lock (s_UniqueCounterLock)
    {
        return s_UniqueCounter;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM