Linux中允许的c/c ++互斥锁最大数量 [英] c/c++ maximum number of mutexes allowed in Linux

查看:64
本文介绍了Linux中允许的c/c ++互斥锁最大数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力尝试找出Linux上不成功的c/c ++进程最大互斥量.另外,还有一种方法可以修改此号码.我正在阅读的书提到了如何找到Linux允许的最大线程数以及如何修改该数字,但没有提到互斥体.

I've been searching trying to find out what is the maximum number of mutexes in Linux for a c/c++ process without success. Also, is there a way to modify this number. The book I'm reading mentions how to find the max number of threads allowed in Linux and how to modify this number but no mention of mutexes.

推荐答案

选中此 pthread_mutex_init .

为什么没有定义限制

考虑了为互斥锁和条件变量的最大数量定义符号,但由于这些对象的数量可能会动态变化,因此被拒绝.此外,许多实现将这些对象放置到应用程序内存中.因此,没有明确的最大值.

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.

<小时>编辑:在您询问有关 mutex 可能具有而非内存的费用的注释中.好吧,我不知道,但是我发现了一些有趣的材料:


In the comments you asked about the costs a mutex may have other than memory. Well, I don't know, but I found some interesting material about that:

此文章位于互斥量的工作方式谈到费用:

费用

关于互斥量的成本,有一些兴趣点.第一个也是非常重要的一点是等待时间.您的线程只应花费很少的时间等待互斥体.如果他们等待太频繁,那么您将失去并发性.在最坏的情况下,许多线程始终尝试锁定同一互斥锁可能会导致性能低于服务所有请求的单个线程.这确实不是互斥量本身的代价,而是并发编程的一个严重问题.

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.

互斥锁的开销成本与测试设置操作和实现互斥锁的系统调用有关.测试和设置成本可能非常低;对于并发处理至关重要,CPU 有充分的理由使其高效.不过,我们还是忽略了另一项重要说明:围栏.这在所有高级互斥锁中都使用,并且其成本可能高于测试设置操作.然而,系统调用比这更昂贵.不仅使您遭受系统调用的上下文切换开销,而且内核现在还花费一些时间在其调度代码中.

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 内部内核结构.也许两者都有.也许是内核错误...我真的不知道.

So I'm guessing the costs they talk about on the EAGAIN error involves either the CPU or internal kernel structures. Maybe both. Maybe some kernel error... I honestly don't know.

我选择了一些您可能会感兴趣的SO Q& A.好看!

I picked some SO Q&A that might interest you. Good reading!

这篇关于Linux中允许的c/c ++互斥锁最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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