简体   繁体   English

防止或禁止使用Parallel.ForEach

[英]Prevent or forbid use of Parallel.ForEach

I wrote a library on top of System.Data.SQLite and realized it (my library) has unpredictable behaviour when using Parallel.ForEach . 我在System.Data.SQLite之上编写了一个库,并意识到使用Parallel.ForEach时该库(我的库)具有不可预测的行为。 I might debug this eventually (ie if I get/take the time), most likely by locking the right parts, but for now let's say I want to just prevent usage of Parallel.ForEach , or force usage of my library to allow (or result in) only a single thread, how would I proceed? 我可能最终会对此进行调试(即,如果我花时间/花点时间),很可能是通过锁定正确的部分来进行的,但是现在让我们说,我只是想防止使用Parallel.ForEach ,或强制使用我的库以允许(或结果)只有一个线程,我将如何进行?

You can't control how your API is consumed by external code. 您无法控制外部代码如何使用您的API。 If it's something you absolutely cannot address prior to release, it would be a good idea to be very explicit about failure cases in your documentation (both XML comments and any sort of "help file"). 如果您在发布之前绝对无法解决此问题,那么最好在文档中明确说明失败案例(包括XML注释和任何形式的“帮助文件”)。

A few quick threadstatic attributes might solve your concurrency problem, but this smells like the tip of a much larger iceberg. 一些快速的线程静态属性可能会解决您的并发问题,但这闻起来像是更大的冰山一角。 Fix the root cause, not the symptom. 解决根本原因,而不是症状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM