首页 \ 问答 \ for循环生成的工作线程列表超出了循环迭代次数(List of worker threads spawned by for loop is exceeding for-loop iterations)

for循环生成的工作线程列表超出了循环迭代次数(List of worker threads spawned by for loop is exceeding for-loop iterations)

我对于正确的线程我是相对绿色的,我有这个,它是为了简单而修改但它基本上是相同的东西:

//Global:
N=2
bool[] mySwitches;

//In my main:

mySwitches = new bool[N];
for (int i = 0; i < N; i++)
{
    ThreadList.Add(new Thread(() => Worker(i)));
    ThreadList[i].Start();
}

//Outside of main:

Private Void Worker(int num)
{
  while(true)
  {
    if (mySwitches[num]) //error happes here because num is equal to N, how?
    {
        //do something
    }
  }
}

如上所示,工作线程以某种方式得到num = N的值,我希望它只能达到N-1。 我知道在创建Worker之后我会增加,是不是以某种方式通过引用而不是值传递?

我尝试通过在我的while循环之前进行测试来修复问题,如果num = N则返回,但即使使用此规定,我也会得到相同的错误。 这让我相信在线程启动后,num会以某种方式递增。

我通过在ThreadList [i] .Start()之后直接放置Sleep(20)来解决这个问题,但我真的不喜欢使用Sleep,很明显我不知道这个线程场景是如何工作的。

任何人都可以对此有所了解吗?


I am relatively green to proper threading, I have this, it's modified for simplicity but it is essentially the same thing:

//Global:
N=2
bool[] mySwitches;

//In my main:

mySwitches = new bool[N];
for (int i = 0; i < N; i++)
{
    ThreadList.Add(new Thread(() => Worker(i)));
    ThreadList[i].Start();
}

//Outside of main:

Private Void Worker(int num)
{
  while(true)
  {
    if (mySwitches[num]) //error happes here because num is equal to N, how?
    {
        //do something
    }
  }
}

As shown above, somehow a worker thread gets a value of num=N, I expect it to only reach N-1. I know that i will get incremented after the Worker is created, is i somehow getting passed by reference instead of value?

I tried to fix the issue by putting a test before my while loop to return if num=N, but even with this provision I get the same error. This leads me to believe that num is incremented somehow after the thread starts.

I fixed this issue by putting a Sleep(20) directly after ThreadList[i].Start(), but I don't really like using Sleep, and it's clear I don't know how this threading scenario actually works.

Can anyone shed any light on this?


原文:https://stackoverflow.com/questions/32073961
更新时间:2023-10-04 14:10

最满意答案

为了加载Disqus嵌入,它必须来自有效的引用者 - 通常这个引用者在手机上就像文件系统路径一样。

处理此问题的理想方法是将Disqus嵌入在外部页面上并将其加载到视图中。 您甚至可以使用此示例在静态cdn上加载它: https//github.com/disqus/DISQUS-API-Recipes/tree/master/mobile/js


In order for the Disqus embed to load, it must come from a valid referrer - typically this referrer when on the phone is something like the file system path.

The ideal way to handle this is to host the Disqus embed on an external page and load that into your view. You can even load this on a static cdn using this example: https://github.com/disqus/DISQUS-API-Recipes/tree/master/mobile/js

相关问答

更多
  • 以上代码在Android设备上运行良好。 如果我们在模拟器上运行它,只会出现问题,因为与设备相比,模拟器速度很慢...... Well the above code runs well on Android Devices. Only the problem occurs if we run it on Emulator, as Emulator are slow as compared to Devices...
  • 这是我们必须为您的应用程序启用的。 但是,我建议托管一个页面来处理标准服务器端流程的身份验证。 然后,您可以在用户授权后将访问令牌和其他变量从页面中提取到您的应用程序中。 原因是您不必处理表单验证,错误消息传递,并且可以利用我们对表单的更新而无需触及您的代码。 可以在此页面上找到PHP和Python中的一些服务器端OAuth示例: https : //github.com/disqus/DISQUS-API-Recipes/tree/master/oauth This is something we'd h ...
  • 我遇到了同样的问题,让我的网站disqus线程与我的android应用程序链接起来。 如果你有兴趣,我会写一段小小的演练,我会链接到下面的演练。 基本上在你的Android应用程序中,你想使用WebView并使用一个单独的php文件,它可以带上你的disqus标识符。 http://globeotter.com/blog/disqus-android-code/ I ran into the same problem with having my web site disqus threads linked ...
  • 我写了一篇博客,详细介绍了通过WebView实现这一目标的方法 http://globeotter.com/blog/disqus-android-code/ 希望能帮助到你, I wrote a blog detailing a way to do it via a WebView http://globeotter.com/blog/disqus-android-code/ Hope it helps,
  • 让我们确保一切都正确设置。 我经历了基于文档的测试应用程序,没有任何问题。 的Gemfile gem 'disqus_rails' 现在 bundle install 创建初始化程序 disqus_rails.rb (如文档中所述): DisqusRails.setup do |config| config::SHORT_NAME = "your_short_name" config::SECRET_KEY = "your_secret_disqus_key" config::PUBLIC_K ...
  • 为了加载Disqus嵌入,它必须来自有效的引用者 - 通常这个引用者在手机上就像文件系统路径一样。 处理此问题的理想方法是将Disqus嵌入在外部页面上并将其加载到视图中。 您甚至可以使用此示例在静态cdn上加载它: https : //github.com/disqus/DISQUS-API-Recipes/tree/master/mobile/js In order for the Disqus embed to load, it must come from a valid referrer - ty ...
  • 这是一个很好的解决授权问题并提供无缝流程来显示和响应评论。 希望它有助于https://github.com/moqod/disqus-ios Figured out that for new accounts, it was going to disqus.com/_ax/facebook/complete then immediately after that to disqus.com/next/register, thus never giving new accounts a change to ...
  • 我发现Disqus只会使用博客的帖子URL来跟踪评论帖子,所以使用Blogger API ,你可以获得帖子的URL ,只需在通用的Disqus脚本中设置this.page.url ,它就会显示相同的内容。来自特定博客文章的评论。 I found out that Disqus will use the Blog's post URL only to track the comment thread, so using the Blogger API, you can get the post's URL a ...
  • 让我感到震惊的是iOS没有disqus库,所以我自己制作了一个简单的包装器。 目前,它仅用于查看和发布评论。 随意使用它: Disquser 。 It struck me that there's no disqus library for iOS, so I made a simple wrapper myself. Currently, it's only for viewing and posting comments. Feel free to use it : Disquser.

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)