c# - Run any number of functions, each on their own thread -
what efficient way of performing same function on collection of objects, each running in parallel? know new thread(() => myfunc(myparam)).start() in loop, there better way? seems ugly way of doing it.
[...] there better way? seems ugly way of doing it.
yes, ugly , inefficient way of doing it. each thread consume lot of resources computer can execute n threads @ same time n number of cpu cores on computer. instead of using low-level primitive thread can build on top of libraries optimize number of threads fit number of cpu cores.
in case have collection of objects suggest:
linq code:
var result = source.select(item => ...).tolist(); parallel linq code:
var result = source.asparallel().select(item => ...).tolist(); for each loop:
foreach (var item in source) process(item); parallel each loop:
parallel.foreach( source, item => process(item); ); for loop:
for (var = 0; < list.count; += 1) list[i] = process(list[i]); parallel loop:
parallel.for( 0, list.count, => { list[i] = process(list[i]); } );
Comments
Post a Comment