Laurent Esnault, VP of Engineering, explains the difference between cooperative and preemptive processes during his Break Out Session about Preemptive Multi-threading at 4D Summit 2016. Thanks to Preemptive Multi-threading, you can have more parallel processes, and thus add more and more users, either for desktop or for web applications.
If you missed Laurent’s session at 4D Summit 2016, watch this video and enjoy some personal training by the master of preemptive multi-threading!
Difference between cooperative and preemptive
So what is cooperative and what is preemptive?
The cooperative mode is generally what you are using today with your 4D processes. Cooperative means that the 4D processes cooperate (work together) to share a single CPU. Sometimes they cooperate explicitly, like when you use the IDLE command, and sometimes they cooperate implicitly, like when you use most any other 4D language command. This is because, internally, 4D uses the IDLE command to give the CPU to another process. As a consequence, only one CPU core can be used at a time.
Of course, you all know the 4D processes you are using today. And it seems natural to use those processes to access the user interface. You can open a window, display a dialog or a form, etc. But actually, it’s not at all natural for an operating system. Most operating systems forbid access to the user interface from any thread other than the main thread. But for cooperative processes, 4D internally manages access to the user interface…even in a secondary thread.
In preemptive mode, you don’t have to call the IDLE command, because the 4D processes don’t need to cooperate in order to share the CPU: it’s done by the operating system. The operating system preemptively gives the CPU to one thread or another. So if you have multiple CPUs in your machine, then several threads can be executed at the same time. Therefore, the benefit is that you can use all of your machine’s cores. But from a preemptive thread, you cannot access the user interface.
You can see how in cooperative mode, the processes are sequentially executed, one after another and using only a single CPU whereas in preemptive mode, the operating system dispatches the two processes on all CPUs (in the example shown, both CPU 1 and CPU 2).
The benefit is obvious. The global execution time is shorter in preemptive mode when you have two CPUs. Even though the execution time of each individual process is the same. Preemptive scheduling doesn’t result in performance gain in the execution time of a process. It’s the global execution time of multiple tasks, running in multiple processes, that will be reduced.
Here is a very simple demonstration. Each button creates four individual processes.
The first button is creating four cooperative processes to do some nonsense computations (strictly for the demonstration purposes). The global execution time is 643 ticks and the execution time of each individual process is around 150 ticks. So basically, the global execution time is just the addition of the four individual execution times.
If we do the exact same computation in four preemptive threads, you’ll see that the execution time of the individual processes is roughly the same (150 ticks) however, the global execution time is FOUR TIMES shorter.
Find out the example database used for the demonstration and complementary explanations in Vanessa’s post about preemptive processes.