How many mutex objects can exist at the same time?

Table of Contents

Okie, for this question, I remember that a interviewer has asked me before, unfortunately I didn’t know how to answer this question at that time. But the answer he gave me is not very correct when I know the truth.

First, let’s go through the truth: There is no explicit maximum count for the number of mutex objects which may exist simultaneously within an application or on a system in general. Considering this and the fact that mutexes are not required to be created at the kernel level and kernel specific resources are not consumed, most systems are normally limited only by the amount of memory available.

So, it is limited by memory, but not kernel, if there is no enough available memory for us, we can not alloc a new lock. For some hash table structure, if we want to avoid concurrent, we can alloc a lock per key if there is still enough memory available. But lock is still cost memory, if the concurrent is not very high, or the memory is very limit for us, we need to attend it. For this solution, we can alloc limited locks at start, so the collision probability is “hash(key) / locks”, in the best situation, there is no collision occur, the worst situation, all keys will be blocked by one lock, this need to analysis by online data.

Hope this will be a little bit help.  :D