Click here to Skip to main content
13,287,315 members (49,864 online)
Rate this:
Please Sign up or sign in to vote.

I have developed an application in which i am using Thread Pool & Parallel for loop
with lock.
But when i use lock , it slower downs the process of Thread Pool & Parrallel for loop.

Please help me.

Thanks in advance
Posted 21-Mar-11 5:21am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Without seeing the code isn't that what's expected to happen. If you use lock, you prevent simultaneous operations from acquiring the same lock until the lock is released. So you're queueing for the lock.
Mukesh P Prajapati 22-Mar-11 3:06am
i am using the following code:

void DoSomeWork(object state)
lock (lockThis)
DataRow dr = (DataRow)state;
string uri = dr["PingURL"].ToString();
string BuyerId = dr["BuyerId"].ToString();
string PostT = dr["PostType"].ToString();
string ResponseT = dr["ResponseType"].ToString();
FirstTask(xmlData, uri, BuyerId, PostT, ResponseT);
Mika Wendelius 23-Mar-11 15:46pm
If you're running this in a parallel loop, you'll actually serialize the operation since only one thread can enter the lock at a time. If the state passed to the method isn't shared by the threads in the loop, you could remove the lock.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

While using C#4 [Assumed from the tag!] , I don't think that you have to worry about locking the objects / Threadpool. You can make use of Parallel.For[^] method.

For more details search for PLINQ and TPL

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171207.1 | Last Updated 21 Mar 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100