草庐IT

go - sync.Mutex.Lock 是 FIFO 吗?

coder 2023-06-27 原文

如果很多线程锁定在 mutex 上它们是按 FIFO 顺序排队,还是 goroutine 在解锁时获取锁有一定的随机性?

最佳答案

来自source :

// Mutex fairness.
//
// Mutex can be in 2 modes of operations: normal and starvation.
// In normal mode waiters are queued in FIFO order, but a woken up waiter
// does not own the mutex and competes with new arriving goroutines over
// the ownership. New arriving goroutines have an advantage -- they are
// already running on CPU and there can be lots of them, so a woken up
// waiter has good chances of losing. In such case it is queued at front
// of the wait queue. If a waiter fails to acquire the mutex for more than 1ms,
// it switches mutex to the starvation mode.
//
// In starvation mode ownership of the mutex is directly handed off from
// the unlocking goroutine to the waiter at the front of the queue.
// New arriving goroutines don't try to acquire the mutex even if it appears
// to be unlocked, and don't try to spin. Instead they queue themselves at
// the tail of the wait queue.
//
// If a waiter receives ownership of the mutex and sees that either
// (1) it is the last waiter in the queue, or (2) it waited for less than 1 ms,
// it switches mutex back to normal operation mode.
//
// Normal mode has considerably better performance as a goroutine can acquire
// a mutex several times in a row even if there are blocked waiters.
// Starvation mode is important to prevent pathological cases of tail latency.

因此不能仅通过使用互斥锁来保证跨线程的顺序。

关于go - sync.Mutex.Lock 是 FIFO 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51140111/

有关go - sync.Mutex.Lock 是 FIFO 吗?的更多相关文章

  1. ruby-on-rails - Rails、已安装的 Gem 版本和 Gemfile.lock 版本 - 2

    我有一个问题。如果bundle更新,Gemfile.lock总是被提交。今天,当我部署web应用程序时,我看到了这条错误消息,Couldnotfindjwt-1.5.3inanyofthesourcesjwt-1.5.2安装在已部署服务器的共享gem路径中。我认为bundleinstallnewjwt-1.5.3gem因为Gemfile.lock中指定的版本(1.5.3)和安装的版本(1.5.2)不相等。为什么不能bundleinstalljwtgem?如果找不到jwt-1.5.3,是否应该捆绑安装那个gem? 最佳答案 删除Gem

  2. ruby-on-rails - Textmate 'Go to symbol' 相当于 Vim - 2

    在Railcasts上,我注意到一个非常有趣的功能“转到符号”窗口。它像Command-T一样工作,但显示当前文件中可用的类和方法。如何在vim中获取它? 最佳答案 尝试:helptags有各种程序和脚本可以生成标记文件。此外,标记文件格式非常简单,因此很容易将sed(1)或类似的脚本组合在一起,无论您使用何种语言,它们都可以生成标记文件。轻松获取标记文件(除了下载生成器之外)的关键在于格式化样式而不是实际解析语法。 关于ruby-on-rails-Textmate'Gotosymbol

  3. ruby-on-rails - Rails add_index 算法 : :concurrently still causes database lock up during migration - 2

    为了防止在迁移到生产站点期间出现数据库事务错误,我们遵循了https://github.com/LendingHome/zero_downtime_migrations中列出的建议。(具体由https://robots.thoughtbot.com/how-to-create-postgres-indexes-concurrently-in概述),但在特别大的表上创建索引期间,即使是索引创建的“并发”方法也会锁定表并导致该表上的任何ActiveRecord创建或更新导致各自的事务失败有PG::InFailedSqlTransaction异常。下面是我们运行Rails4.2(使用Acti

  4. ruby - Cucumber 测试无法启动,错误为 "Display socket is taken but lock file is missing.." - 2

    运行cucumber后bundleexeccucumberfeatures/emails.feature:20我遇到了错误Displaysocketistakenbutlockfileismissing-checktheHeadlesstroubleshootingguide(Headless::Exception)/Users/me/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/headless-2.2.0/lib/headless.rb:195:inensure_xvfb_is_running'/Users/me/.rbenv/ver

  5. FIFO实战学习-同步FIFO/异步FIFO-格雷码 - 2

    目录FIFO一.自定义同步FIFO1.1代码设计1.2Testbech1.3行为仿真***学习位宽计算函数$clog2()***$clog2()系统函数使用,可以不关注***分布式资源或者BLOCKBRAM二.异步FIFO2.1在FIFO判满的时候有两种方式:2.2异步FIFO为什么要使用格雷码2.2.1介绍格雷码2.2.2格雷码在异步FIFO中的应用2.2.2格雷码判满2.4二进制与格雷码之间的转换2.4.1二进制码转换为格雷码的方法2.4.2格雷码转换为二进制码的方法2.3实现框图2.5实现及仿真代码2.6仿真图验证2.7结论FIFO  这篇更多的是记录FIFO学习,参考了众多优秀的文章,

  6. ruby-on-rails - 未初始化的常量 ActiveSupport::Dependencies::Mutex - 2

    我尝试运行一个从OSX到Debian的工作Rails项目。我在两个系统上都使用RVM并为项目创建了相同的gemsets和rvmrc。在Debian上,我只安装了带有rvm的ruby​​,不存在ruby​​的系统安装。当我进入项目文件夹时,rvm正在切换到版本1.8.7并正在使用项目gemset,一切看起来都很好。但是当我启动rake-T时,我得到了这个错误:$rake-T--trace(in/home/i/project/src)rakeaborted!uninitializedconstantActiveSupport::Dependencies::Mutex/home/i/.rvm

  7. ruby-on-rails - Rails 4.0.3 使用 asset_sync 生成不正确的 Assets 路径 - 2

    我以前多次使用asset_syncgem并取得了很大的成功,但是在Rails4.0.3项目中使用它似乎导致了问题。Assets被上传、散列并gzip到目标目录(我只是使用默认的“Assets”),但是在暂存/生产环境中运行应用程序时,路径不正确。它们的形式是:S3_DOMAIN.com/stylesheets/application.css代替:S3_DOMAIN.com/assets/application-HASH.css有没有人遇到过这个问题?我发现扭转这种行为的唯一方法是将config.assets.compile设置为true,但这在生产环境中行不通。这里是相关的配置文件:#

  8. ruby-on-rails - 用于 Ruby 的 vim 中的全局 "Go to definition"? - 2

    自97年以来我一直在使用vi/vim进行各种快速编辑和管理任务,但最近才考虑使用它来替换Netbeans作为我选择的ruby​​编辑器。我发现一件事在Netbeans和Eclipse中非常有用的是Ctrl+Click“转到定义”功能,您可以在其中按住Ctrl键并单击一个类或方法,然后它将带您了解定义。现在,我玩过丰富的ctags和rails.vim,而且很接近,但没有雪茄。这就是我想要的:默认情况下在Netbeans和Eclipse中,您可以在本地rails中按住ctrl并单击本地方法或类项目,但你也可以ctrl+click定义在gems或用Ruby编写的系统库。以Netbeans为例

  9. ruby - 特拉维斯报告损坏的 Gemfile.lock 的奇怪消息 - 2

    我使用bundler来安装东西,因为我添加了Gemfile.lock,travis开始提示:YourGemfile.lockiscorrupt.ThefollowinggemismissingfromtheDEPENDENCIESsection:'echoe'当然,一切都在本地运行。它也可以使用DeployBot。我什至安装了dockerubuntu并尝试了,仍然可以。我的Gemfile.lock没有损坏。使用相同版本的ruby​​和bundler。这是怎么回事?更新这与bundler版本有关。我使用的是1.11.0,但收到报告说它可以与eg一起使用。1.8.3.??

  10. ruby-on-rails - Rails 上的 ruby : How to have multiple submit buttons going to different methods (maybe with with_action? ) - 2

    这个问题在这里已经有了答案:HowdoIcreatemultiplesubmitbuttonsforthesameforminRails?(7个答案)关闭9年前。所以..'save'%>'library'%>然后在我的Controller中:with_actiondo|a|a.savedoenda.librarydoendend问题是只有一个操作被调用...两个submit_tags调用相同的操作...知道为什么吗?或者我如何获得两个按钮以将表单提交给两种不同的方法?

随机推荐