Sunday, 15 April 2012

c# - Is it the correct implementation? -



c# - Is it the correct implementation? -

i having windows service needs pick jobs database , needs process it.

here, each job scanning process take approx 10 mins complete.

i new task parallel library. have implemented in next way sample logic:

queue queue = new queue(); (int = 0; < 10000; i++) { queue.enqueue(i); } (int = 0; < 100; i++) { task.factory.startnew((object info ) => { var objdata = (queue)data; console.writeline(objdata.dequeue()); console.writeline( "the current thread " + thread.currentthread.managedthreadid); }, queue, taskcreationoptions.longrunning); } console.readline();

but, creating lot of threads. since loop repeating 100 times, creating 100 threads.

is right approach create many number of parallel threads ?

is there way limit number of threads 10 (concurrency level)?

an of import factor remember when allocating new threads os has allocate number of logical entities in order current thread run:

thread kernel object - object describing thread, including thread's context, cpu registers, etc thread environment block - exception handling , thread local storage user-mode stack - 1mb of stack kernel-mode stack - passing arguments user mode kernel mode

other that, number of concurrent threads may run depend on number of cores machine packing, , creating amount of threads larger number of cores machine owns start causing context switching, in long run may slow work down.

so after long intro, stuff. want limit number of threads running , reuse them much possible.

for kind of job, go tpl dataflow based on producer-consumer pattern. little illustration of can done:

// bufferblock equivalent of concurrentqueue buffer objects var bufferblock = new bufferblock<object>(); // actionblock process each object , var actionblock = new actionblock<object>(obj => { // stuff objects bufferblock }); bufferblock.linkto(actionblock); bufferblock.completion.continuewith(t => actionblock.complete());

you may pass each block executiondataflowblockoptions may limit bounded capacity (the number of objects within bufferblock) , maxdegreeofparallelism tells block number of maximum concurrency may want.

there illustration here started.

c# task-parallel-library long-running-processes

No comments:

Post a Comment