4D Blog

Home Events Difference between cooperative and preemptive explained by Laurent Esnault at 4D Summit 2016

Difference between cooperative and preemptive explained by Laurent Esnault at 4D Summit 2016

September 4, 2017

Events

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.

blank

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.

Demonstration

Here is a very simple demonstration. Each button creates four individual processes.

blank

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.

Discuss

Tags 4D Summit, Preemptive, Programming, Scalability, Speed

Latest related posts

  • December 7, 2023

    Catch the 4D Summit 2023 Keynote Replay

  • May 17, 2023

    4D Summit 2023. The Ultimate In-Person Experience is Back.

  • August 2, 2021

    Ready for the 4D World Tour Live 2021?

Caroline Briaud
Caroline Briaud
• 4D Product Team Leader •Caroline joined 4D SAS in July, 2013 as an Engineering Manager responsible for new features definition. Caroline helped the Engineering team to put in place the R-release concept, with the new development process definition and its application within 4D’s engineering teams.Caroline is now leading the 4D Product team, mainly made up of Product Owners which are representing the customers within Engineering.Caroline started her career as a software developer in the mobile phone industry. She specialized in program management with an American company in an international context spread across US, India and China.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy