Parallel.for () loopi noto'g'ri ish zarrachalar identifikatorini yaratadi

.Net ilovasi uchun .net dasturini ishlab chiqdim. Bu erda ko'p ishlov berish uchun parallel.for() ilovasini ishlatdim. Quyidagi konfiguratsiyadan foydalandim:

  1. Windows 7
  2. IIS 7.0
  3. Framework v4.0.30319

ParallelOptions() sinfini ishlatganman va bu sinfning "MaxDegreeOfParallelism" xususiyati 5 ga o'rnatildi. "Log.CurrentThread.ManagedThreadId" ni jurnalning faylida qayd qildim. Ushbu dasturni ikki xil muhitda sinab ko'rdim. Dastur ishlab chiqilgan muhitda ishlayotganida u 5 ta identifikatorni yaratadi. Boshqa muhitda ishlayotgan bo'lsa-da, u 6 yoki 7 razryadli identifikatorlarni yaratadi. Ilova faqat 1-muhitda tuziladi. Ikkinchi muhiti sinovdan o'tkazish uchun dasturning nashr etilgan versiyasidan foydalanganman. Nima uchun bu sodir bo'layotganini va uni qanday hal qilishni bilib, kimdir menga yordam bera oladimi?

4

2 javoblar

Parallel.For uses thread pool threads. The MaxDegreeOfParallelism controls the maximum concurrent number of threads being used, not 5 particular threads - the thread pool is at liberty to process work on any of its threads, and Parallel.For makes sure only 5 are running at the same time.

4
qo'shib qo'ydi

Bunga ishonaman, chunki Parallel.For() ishlatiladigan faqat dinamik bo'linadi, shuning uchun ketma-ket uzatilgan bo'lishi mumkin va har bir bo'lim alohida Task/Mavzu:

  1. MSDN: How to: Implement Dynamic Partitions

    Each time a partition calls MoveNext on the enumerator, the enumerator provides the partition with one list element. In the case of PLINQ and ForEach, the partition is a Task instance. Because requests are happening concurrently on multiple threads, access to the current index is synchronized.

    (I believe the same is true for Parallel.For())

  2. MSDN: Custom Partitioners

    PLINQ supports a fixed number of partitions (although data may be dynamically reassigned to those partitions during run time for load balancing.). For and ForEach support only dynamic partitioning, which means that the number of partitions changes at run time. For more information, see Custom Partitioners for PLINQ and TPL.

2
qo'shib qo'ydi