One day, I was busy with a consulting project, and suddenly I encountered a thread synchronization problem that I had never encountered before. The company is creating a WEB service and almost all client requests received by the server require read-only access to some shared data. Sometimes, you'll also receive requests to modify shared data, and we need to keep that data synchronized. It sounds like a good fit to use a read-write lock.
However, after a more in-depth study of the project, I learned that the company's requirements have an unusual place: When the server receives a WEB service request and the lock status is write, the lock lasts a very long time. When a write thread modifies shared data, other WEB service requests that read the data need to be entered into the server. The problem here is that the threads in the pool are being awakened to handle read requests, but all read threads will quickly go to sleep because they have to wait for the write thread to complete their task.
Creating hundreds of pools of threads does not take a long time, and all of these threads are in the waiting state within the operating system kernel. Many threads are unable to run at this time, but simply waste resources (such as the stack of each thread and the associated thread kernel object). It's bad. And it's getting worse! When a write thread completes the write operation of the shared data, it unlocks. The lock then sees hundreds of read threads waiting for it to release all read threads that are in the waiting state. The server computer has four CPUs, and these four small CPUs must handle the work of all these running threads. Windows® uses its normal polling scheduling technology to schedule all of these threads, but the overhead of switching the CPU from one thread to another can significantly reduce performance, scalability, and throughput.
This problem has been bothering me for months, until finally I designed my own thread sync lock readerwritergate, the problem was solved. This lock is very efficient and can be used very well in a variety of applications. The interesting thing about ReaderWriterGate is that the calling threads are almost never interrupted, and even if they are interrupted, they can be guaranteed to only break for a very short period of time. and internally, because ReaderWriterGate uses one of my resourcelock derived classes (discussed in the previous column in June), I only change a single line of code to allow readerwritergate to use internal rotation locks to In the Windows kernel, threads never break (as I described in the October 2005 "Concurrency Events" column). This can improve performance, but for the sake of fairness, it requires that all threads using readerwritergate have the same thread priority and disable the priority Elevation feature. This will further improve performance. In addition, ReaderWriterGate can do a lot of work with only a few threads. Therefore, no matter how many requests are made to the Gate, very few resources are required.
I implemented this feature in my own way and offered it as part of the Power Threading library for free, and the reader can download it from www.wintellect.com. If you want to take advantage of my implementation, follow the end User License Agreement (EULA) that is included with the library. Please refer to the EULA for more details.
Next I'll introduce the principles behind readerwritergate, explore a possible approach, and provide a new way to study threading and thread synchronization. I want you to see where you can apply such ideas in your existing code to redesign the architecture as little as possible, and you can combine some of these ideas to improve the performance and scalability of your application.
ReaderWriterGate Object Model
To use the ReaderWriterGate class, you only need to understand the following methods:
public sealed class
readerwritergate
{public
readerwritergate ();
public void Queuewrite (
readerwritergatecallback callback, Object state);
public void Queueread (
readerwritergatecallback callback, Object state);
...//Some methods not shown to simplify the discussion
}