c / c Linux中允许的最大互斥量数

前端之家收集整理的这篇文章主要介绍了c / c Linux中允许的最大互斥量数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找试图找出 Linux中c / c进程的最大互斥量是多少而没有成功.还有,有办法修改这个数字.我正在阅读的书中提到了如何找到Linux中允许的最大线程数以及如何修改此数字但没有提到互斥锁.

解决方法

检查这个 pthread_mutex_init.

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

原文链接:https://www.f2er.com/c/119579.html

猜你在找的C&C++相关文章