Share Nothing
Reduction in sharing is good, but nothing beats eliminating contention altogether. Instead of sharing a resource, you can sometimes eliminate sharing completely by giving each thread its own private resource. Recall from the previous section that each Web server worker thread (in our implementation) maintained a pointer to its private data object of type ThreadSpecificData:
class ThreadSpecificData { HTRequest *reqPtr; // Attributes of current HTTP request ... };
The reqPtr member was a pointer to an HTRequest object containing information about the current HTTP request. At the beginning of each request, the worker thread created a brand new object of type HTRequest:
ThreadSpecificData *HTInitRequest(StreamSocket sock) { ... ...
Get Efficient C++ Performance Programming Techniques now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.