Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
hi,

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

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.
 
Share this answer
 
Comments
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);
}
}
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.
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900