锁的性能/使用2个问题 [英] 2 questions on locks performance/use

查看:76
本文介绍了锁的性能/使用2个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在服务器上的应用程序,我需要分配给每个连接的客户端的唯一的ID,所以我做这种方式:

On the server application, I need to assign to each connected client an unique ID, so I am doing it this way:

private short GetFreeID()
{
    lock (this.mUsedPlayerIDsSynchronization)
    {
        for (short I = 1; I < 500; I++)
        {
            if (ClientIDPool[I] == false)
            {
                ClientIDPool[I] = true;
                return I;
            }
        }
        return -1;
    }
}



我的第一个问题:难道是更有效地完成,我的意思是有更好的性能?我在这里读了,我们应该学会不锁编写代码。我也看到有一些原子操作还有其他的选择。
第二个问题:如果我想锁定全班同学为了不容许范围内做任何改变?例如:一个客户会更新第二个客户端的数据,我可以锁定整个第二客户端类,它是绝对的封锁?我仍然认为锁才会确保它的片断里面的那个代码是由只有一个线程的时候进入的,所以我不知道锁(客户端2)会导致没有在这个类是可以改变的,直到这个锁是释放。

My first question: Could it be done more efficiently, I mean with better performance? I have read here that we should learn to write code without locks. I have also read there for some atomic operations there are other options. Second question: What if I wanted to lock the whole class in order to do not allow to make any changes within? For example: one client will update second clients data, can I lock the whole second client class that it is absolutely blocked? I still think "lock" will only make sure that code inside its snippet is entered by only one thread at the time, so I do not know if "lock(client2)" causes that nothing in that class can be changed until this lock is released.

推荐答案

锁常常是的简单的把事情做对,这是非常重要的途径。常常也没关系,如果有一个做事的更有效的方式,只要你有清晰的代码,它执行的不够好

Locks are often the simplest way of getting things right, which is very important. Quite often it doesn't matter if there's a more efficient way of doing things, so long as you've got clear code and it performs well enough.

下面一个更高性能的方法,但是,这是可生成一个随机的GUID,或者如果你的的要重用的ID,有一个池(如 LinkedList的)未使用的ID。然后,您可以从池中取得很快,和ID(再次快速)返回到池中,一旦你就大功告成了。

A more performant approach here, however, would be to either generate a random GUID, or if you do want to reuse IDs, have a "pool" (e.g. a LinkedList) of unused IDs. You could then take from the pool very quickly, and return the ID to the pool (again quickly) once you're done.

另外,如果你真的只需要一个整数,它没有的有无的是低的,你可以有它从0开始,你刚刚增加每次一个静态变量 - 你可以做到这一点,而不使用的 Interlocked.Increment 如果您希望。我怀疑你会用完的64位整数,例如:)

Alternatively, if you really just need an integer and it doesn't have to be a low one, you could have a static variable which starts at 0 and which you just increment each time - you can do this without locking using Interlocked.Increment should you wish. I doubt that you'll run out of 64-bit integers, for example :)

至于你的第二个问题:是的,锁是咨询。如果在类内一切取出同一锁改变任何字段之前(和领域是私有的)然后,以防止其他代码从失常...但每个代码位的确实的需要取出锁

As for your second question: yes, locks are advisory. If everything within the class takes out the same lock before changing any fields (and the fields are private) then that prevents other code from misbehaving... but each bit of code does need to take out the lock.

编辑:如果你真的只需要,我还是会建议只使用 Interlocked.Increment 整数 - 即使如果你的流量增加了1000倍,你可以使用一个64位的整数来代替。但是,如果你想重用的ID那么我建议创建一个新的类型来表示的池子。给出的的有多少已经被创建的,所以,如果你用完了,你可以指定一个新项目的计数器。然后,只需保存,可用的在问答LT; INT> 的LinkedList< INT> 堆叠式和LT; INT> (它不是要多大意思,你使用)。假设你能相信自己的代码来合理回报的ID,就可以使API简单:

If you really only need an integer, I would still suggest just using Interlocked.Increment - even if your traffic increases 1000-fold, you could use a 64 bit integer instead. However, if you want to reuse IDs then I'd suggest creating a new type to represent the "pool". Give that a counter of how many have been created, so that if you run out you can assign a new item. Then just store the available ones in a Queue<int>, LinkedList<int> or Stack<int> (it's not going to matter very much which you use). Assuming you can trust your own code to return IDs sensibly, you can make the API as simple as:

int AllocateID()
void ReturnID(int id)

AllocateID 会检查,查看是否池是空的,如果是这样分配一个新的ID。否则,它只是从池中删除的第一个条目并返回。 ReturnID 只会指定ID添加到池中。

AllocateID would check to see if the pool is empty, and allocate a new ID if so. Otherwise, it would just remove the first entry from the pool and return that. ReturnID would just add the specified ID to the pool.

这篇关于锁的性能/使用2个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆