Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preemptive Multithreading in Delphi

I've read about Preemptive Multithreading here and here.

Is there a way to do this in Delphi and how does this compare (advantages and disadvantages) to other methods of threading in Delphi?

like image 962
lkessler Avatar asked Dec 10 '22 12:12

lkessler


2 Answers

The "other methods" you're referring to all seem to be using the operating system's underlying threading capability -- which is preemptive. In other words, choose whichever you find most convenient, and it'll be preemptive.

Getting non-preemptive (aka cooperative) threading requires a bit of extra work, typically by converting threads to "fibers".

like image 144
Jerry Coffin Avatar answered Jan 02 '23 06:01

Jerry Coffin


Modern versions of Windows are all preemptive multitasking operating systems. This means that threads and processes (where a process to exist requires at least one thread of execution) are all scheduled and preemptively run.

So "is there a way to do this in Delphi" has the following answers:

  • Your singlethreaded Delphi application is already preemptively scheduled with the other applications
  • If you write a multithreaded Delphi application, it also will be. You would have to go to considerable effort to write a non-preemptive model, such as a cooperative threading model in your application. One approach might be to use coroutines; here is an example using Delphi 7.

The best answer is use TThread or any native Windows thread or wrapper around them. You will have preemptive multithreading.

All the models in your link use normal Windows threads and I suspect your question means you're confused about different threading techniques, which are mostly techniques for communication or running tasks (jobs of work that are run on other threads.) If this is the case, you might want to either update your question or ask another looking for an explanation of these models.

like image 44
David Avatar answered Jan 02 '23 04:01

David