解决方法
Why No Limits are Defined
Defining symbols for the maximum number of mutexes and condition variables was considered but rejected because the number of these objects may change dynamically. Furthermore,many implementations place these objects into application memory; thus,there is no explicit maximum.
编辑:在评论中,您询问了互斥量可能具有的内存以外的成本.嗯,我不知道,但我发现了一些有趣的材料:
这篇关于费用的文章在How does a Mutex Work上说了这个:
The Costs
There are a few points of interest when it comes to the cost of a mutex. The first,and very vital point,is waiting time. Your threads should spend only a fraction of their time waiting on mutexes. If they are waiting too often then you are losing concurrency. In a worst case scenario many threads always trying to lock the same mutex may result in performance worse than a single thread serving all requests. This really isn’t a cost of the mutex itself,but a serIoUs concern with concurrent programming.
The overhead costs of a mutex relate to the test-and-set operation and the system call that implements a mutex. The test-and-set is likely very low cost; being essential to concurrent processing the cpus have good reason to make it efficient. We’ve kind of omitted another important instruction however: the fence. This is used in all high-level mutexes and may have a higher cost than the test-and-set operation. More costlier than even that however is the system call. Not only do you suffer the context switch overhead of the system call,the kernel now spends some time in its scheduling code.
所以我猜他们在EAGAIN错误上讨论的成本涉及cpu或内部内核结构.也许两者.也许有些内核错误……老实说,我不知道.
StackOverflow资源
我选了一些你可能感兴趣的SO Q& A.好读!
> How efficient is locking an unlocked mutex? What is the cost of a mutex?
> How pthread_mutex_lock is implemented
> How do mutexes really work?
> When should we use mutex and when should we use semaphore