I am reading the excellent article on Parallel extensions by Joe Albahari. He says,
Leveraging multiple cores is easy for
most server applications, where each
thread can independently handle a
separate client request, but is harder
on the desktop — because it typically
requires that you take your
computationally intensive code and do
the following:
- Partition it into small chunks.
- Execute those chunks in parallel via multithreading.
- Collate the results as they become available, in a thread-safe and
performant manner.Although you can do all of this with
the classic multithreading constructs,
it’s awkward — particularly the steps
of partitioning and collating. A
further problem is that the usual
strategy of locking for thread safety
causes a lot of contention when many
threads work on the same data at once.The PFX libraries have been designed
specifically to help in these
scenarios.
I am wondering how PFX reduces the contension?
AFAIK, threads are managed by the operating system and OS is responsible for providing and releasing locks. Everything else like PFX are abstractions on top of this which handles the locking well and tries to do the work with very minimal lock. I guess this is possible with even directly manipulating threads and locking carefully to avoid contension.
Is this correct? Or is there a better way to do thread safe operation with out using locks?
A lot of work went into PFX’s partitioning, and also enhancing the
ThreadPoolto have work-stealing queues. Between these two, a lot of contention is eliminated (compared to a standard shared producer/consumer queue approach).