4

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?

Community
  • 1
  • 1
lkessler
  • 19,819
  • 36
  • 132
  • 203

3 Answers3

7

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".

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Thank you for clearing that up for me. What threw me off was that I didn't realize those PM links were for attempted patents from 1994, prior to when Windows implemented it, and I thought they were talking about something new and different from Delphi's threading. – lkessler Nov 19 '10 at 01:23
  • @lkessler: if they'd really been from 1994, they'd probably have been rejected (OS/2 had been out for a while by then). Their priority date is 1982 though. They still might not be valid, but at least there's some possibility. – Jerry Coffin Nov 19 '10 at 04:15
5

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.

David
  • 13,360
  • 7
  • 66
  • 130
2

Have you looked at User-Mode Scheduling which was introduced in Windows 7. Fibers basically don't really work. There's lots of information on this on the MSDN site and I seem to recall a few videos on Channel 9.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • That's interesting and good to know. Unfortunately, I can't consider it until almost all my users are using Windows 7 or later, which will be many years from now. – lkessler Nov 20 '10 at 03:07
  • +1, very interesting. I hadn't heard of it! lkessler, you could probably have a hybrid approach, where your code falls back to standard scheduling if it's not being run on 64-bit Windows 7. – David Nov 21 '10 at 22:54