Click here to Skip to main content
15,913,669 members
Home / Discussions / C#
   

C#

 
QuestionUsing threads to process a collection [modified] Pin
LetMeFinclOut17-May-11 15:03
LetMeFinclOut17-May-11 15:03 
AnswerRe: Using threads to process a collection Pin
Not Active17-May-11 16:30
mentorNot Active17-May-11 16:30 
GeneralRe: Using threads to process a collection Pin
LetMeFinclOut17-May-11 17:13
LetMeFinclOut17-May-11 17:13 
AnswerRe: Using threads to process a collection Pin
Luc Pattyn17-May-11 18:30
sitebuilderLuc Pattyn17-May-11 18:30 
GeneralRe: Using threads to process a collection Pin
LetMeFinclOut18-May-11 0:49
LetMeFinclOut18-May-11 0:49 
GeneralRe: Using threads to process a collection Pin
Luc Pattyn18-May-11 1:59
sitebuilderLuc Pattyn18-May-11 1:59 
GeneralRe: Using threads to process a collection Pin
BobJanova18-May-11 23:16
BobJanova18-May-11 23:16 
AnswerRe: Using threads to process a collection Pin
Luc Pattyn17-May-11 16:30
sitebuilderLuc Pattyn17-May-11 16:30 
Hi,

here are my thoughts on the matter:

1.
I watched the amount of state variables you were using. Method #1 tries to keep track of the number of threads, and needs a number of AutoResetEvents. Method #2 uses an enumerator and a lock, to me that is much simpler to write, to understand, to debug, and hence the preferred way of them both. With the API you have chosen (taking an IEnumerable myCollection) it probably is the best one can do.
Not tested: If myCollection were an IList, you wouldn't need the enumerator and could simply use indexing, which also implies you wouldn't need the lock, a simple Interlocked.Increment() would suffice.
All the above is assuming your collection does not change while the jobs are being executed; if the workload is dynamic, I recommend using a real queue, and a lock of course.

2.
choosing the kind of thread should be easy: BackgroundWorker is based on ThreadPool; if the progress and completion features are appealing, prefer BGW over TP, otherwise don't. There is however one big caveat: ThreadPool has it's own mind about how many threads will actually be used: when you queue 20 jobs, you won't necessarily get 20 threads from the pool right away (other things may be going on in the ThreadPool); when the pool is too busy, it will get extended (within limits) and there is a complex algorithm in place that extends the pool with up to 2 threads per second if necessary; I don't now exactly what rule applies to retiring threads from the pool. This really boils down to your uint maxThreads parameter possibly be of limited value to what is going to really happen. Using instances of Thread puts you fully in charge. On top of that, whatever you do, determining what the optimal number of threads would be isn't very easy as they need to be mapped onto the number of cores in the system. I typically launch no more than N (and never more than 2*N) identical jobs on a system with N cores (see Environment.ProcessorCount).

3.
The value of multi-threading is greatest when the performance limiting factor is computational; threads add processing power to your app, as long as you don't exceed the processor count (i.e. typically Task Manager shows less than 99% CPU load). Quite often adding threads to an app moves the performance bottleneck to some other part of the system, maybe the cache efficiency goes way down and the memory bus bandwidth becomes critical; maybe the disk or network bandwidth becomes the major factor, etc. I recommend making the number of threads a variable, and experimenting with that number while watching total CPU load; as soon as it stops rising, adding threads won't help any further.

Final note: you may not need the actual optimum; there is the rule of diminishing returns, so going from 1 to 2 threads is the biggest step forward (or not!); going from 2 to 4 is a lot easier (you already have the code in place), however it may be much less rewarding. You could easily determine the empirical optimum on a given system; in my experience referring to Environment.ProcessorCount is a good predictive way to deal with varying hardware.

Smile | :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

QuestionListView Pin
wizardzz17-May-11 11:21
wizardzz17-May-11 11:21 
AnswerRe: ListView Pin
gavindon17-May-11 11:24
gavindon17-May-11 11:24 
AnswerRe: ListView Pin
RobCroll17-May-11 16:36
RobCroll17-May-11 16:36 
AnswerRe: ListView Pin
Mycroft Holmes17-May-11 19:47
professionalMycroft Holmes17-May-11 19:47 
AnswerRe: ListView Pin
Shameel18-May-11 0:27
professionalShameel18-May-11 0:27 
GeneralRe: ListView Pin
wizardzz18-May-11 8:24
wizardzz18-May-11 8:24 
QuestionHow to detect mouse over and out on windows title bar Pin
Tridip Bhattacharjee17-May-11 3:43
professionalTridip Bhattacharjee17-May-11 3:43 
AnswerRe: How to detect mouse over and out on windows title bar PinPopular
Dave Kreskowiak17-May-11 4:58
mveDave Kreskowiak17-May-11 4:58 
QuestionSelecting elements from array at specific indices Pin
Chesnokov Yuriy17-May-11 1:25
professionalChesnokov Yuriy17-May-11 1:25 
AnswerRe: Selecting elements from array at specific indices Pin
Luc Pattyn17-May-11 1:54
sitebuilderLuc Pattyn17-May-11 1:54 
AnswerRe: Selecting elements from array at specific indices Pin
Chesnokov Yuriy17-May-11 1:58
professionalChesnokov Yuriy17-May-11 1:58 
GeneralRe: Selecting elements from array at specific indices Pin
Luc Pattyn17-May-11 2:01
sitebuilderLuc Pattyn17-May-11 2:01 
AnswerRe: Selecting elements from array at specific indices Pin
Wayne Gaylard17-May-11 3:48
professionalWayne Gaylard17-May-11 3:48 
AnswerRe: Selecting elements from array at specific indices Pin
Luc Pattyn17-May-11 4:17
sitebuilderLuc Pattyn17-May-11 4:17 
GeneralRe: Selecting elements from array at specific indices Pin
Wayne Gaylard17-May-11 4:24
professionalWayne Gaylard17-May-11 4:24 
GeneralRe: Selecting elements from array at specific indices Pin
Chesnokov Yuriy17-May-11 7:15
professionalChesnokov Yuriy17-May-11 7:15 
GeneralRe: Selecting elements from array at specific indices Pin
Pete O'Hanlon17-May-11 9:17
mvePete O'Hanlon17-May-11 9:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.