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
最满意答案
为了加载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
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
以上代码在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,
-
Rails 4 - 设置Disqus(Rails 4 - Setting up Disqus)[2022-01-13]
让我们确保一切都正确设置。 我经历了基于文档的测试应用程序,没有任何问题。 的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 ...
-
iPhone App中的Disqus API(Disqus API in iPhone App)[2022-07-27]
让我感到震惊的是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.