Click here to Skip to main content
11,410,750 members (64,377 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0 Threading
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 21-Mar-11 5:21am
Rate this: bad
good
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.
  Permalink  
Comments
Mukesh P Prajapati at 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 at 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
good
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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 239
1 Sascha Lefévre 85
2 Dave Kreskowiak 80
3 Maciej Los 68
4 RyanDev 50
0 Sergey Alexandrovich Kryukov 8,885
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 21 Mar 2011
Copyright © CodeProject, 1999-2015
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