April 2018
Intermediate to advanced
300 pages
7h 41m
English
In multithreaded applications, we have shared resources that are accessible by multiple threads executing simultaneously. The area where the resources are shared across multiple threads is known as the critical section. To protect these resources and provide thread-safe access, there are certain techniques that we will discuss in this section.
Let's take an example where we have a singleton class for logging a message into the filesystem. A singleton, by definition, denotes that there should only be one instance shared across multiple calls. Here is the basic implementation of a singleton pattern that is not thread-safe:
public class Logger { static Logger _instance; private Logger() { } public Logger GetInstance() ...