So first I learned that with creating a new thread, we can run the program in parallel...then I learned there are shared resources like Console that we should lock it down so until one thread is not done yet, another thread won't enter. So isn't this in conflict with Parallel threading? Doesn't it go back to a Sync app ?
Asked
Active
Viewed 521 times
0
-
Lock contention blocks other threads so you're not parallel when fighting over the resource. You're parallel when you're not. So the more course grained your locks, the less parallel. The more fine grained, the more parallel you are. For example, locking just around writing a member variable is very fine grained. – bryanmac Jun 18 '14 at 02:20
-
2I strongly recommend that you look at Joe Albahari's site on threading. This'll help you to gain a strong fundamental grasp of the subject. http://www.albahari.com/threading/ – code4life Jun 18 '14 at 02:31
-
Oh Ok so I understood it correctly. Locks temporarily make it not to be parallel. – ConfusedSleepyDeveloper Jun 18 '14 at 02:37
-
1The Console can be used from different threads simultaneously because it already serializes access to its members for you (for more details, see here: http://stackoverflow.com/questions/1079980/calling-console-writeline-from-multiple-threads) – Oleg Jun 18 '14 at 03:05
1 Answers
1
You only need to lock specific resources that aren't thread safe. I highly suggest you take a look at this five part article on multithreaded programming. The second part, Synchronization, talks about locking. Give the whole thing a read, it's very informative.

cost
- 4,420
- 8
- 48
- 80