Lock-less Multi-Threading in Delphi
In this video, I demonstrate a technique for communicating between threads in a Delphi application, without using typical locking mechanisms such as critical sections. I create essentially a ring-buffer based signaling system ( a message pipe ) for sending messages from one thread to another, and then use two such pipes to send messages bidirectionally between two threads.
I feel that while this video is quite valid, I don’t take sufficient time to explain the limitations and dangers of using this system. For instance, the “message pipe” system is intended to send messages from one thread to another. It would not support multiple threads sending, or multiple threads listening at the same time. The nature of “lock-less” multi-threading is such that making a system safe for multiple threads would involve duplication of this system, which frankly might well be a performance bottle neck worse than simply using locks to begin with.
I also make no effort in this example to protect the messaging system against miss-use. Again, this video is demonstrating a method for achieving a lock-less threading environment, which is by its very nature not-safe, and should only be used where the potential performance gains are essential. Otherwise, just go ahead and use locking, you’ll thank yourself.
Best Viewed in Full Screen (Recorded 1080p).