Preemptive threads Vs Non Preemptive threads

LinuxMultithreadingUnixPthreads

Linux Problem Overview


Can someone please explain the difference between preemptive Threading model and Non Preemptive threading model?

As per my understanding:

  • Non Preemptive threading model: Once a thread is started it cannot be stopped or the control cannot be transferred to other threads until the thread has completed its task.
  • Preemptive Threading Model: The runtime is allowed to step in and hand control from one thread to another at any time. Higher priority threads are given precedence over Lower priority threads.

Can someone please:

  1. Explain if the understanding is correct.
  2. Explain the advantages and disadvantages of both models.
  3. An example of when to use what will be really helpful.
  4. If i create a thread in Linux (system v or Pthread) without mentioning any options(are there any??) by default the threading model used is preemptive threading model?

Linux Solutions


Solution 1 - Linux

  1. No, your understanding isn't entirely correct. Non-preemptive (aka cooperative) threads typically manually yield control to let other threads run before they finish (though it is up to that thread to call yield() (or whatever) to make that happen.
  2. Preempting threading is simpler. Cooperative threads have less overhead.
  3. Normally use preemptive. If you find your design has a lot of thread-switching overhead, cooperative threads would be a possible optimization. In many (most?) situations, this will be a fairly large investment with minimal payoff though.
  4. Yes, by default you'd get preemptive threading, though if you look around for the CThreads package, it supports cooperative threading. Few enough people (now) want cooperative threads that I'm not sure it's been updated within the last decade though...

Solution 2 - Linux

Non-preemptive threads are also called cooperative threads. An example of these is POE (Perl). Another example is classic Mac OS (before OS X). Cooperative threads have exclusive use of the CPU until they give it up. The scheduler then picks another thread to run.

Preemptive threads can voluntarily give up the CPU just like cooperative ones, but when they don't, it will be taken from them, and the scheduler will start another thread. POSIX & SysV threads fall in this category.

Big advantages of cooperative threads are greater efficiency (on single-core machines, at least) and easier handling of concurrency: it only exists when you yield control, so locking isn't required.

Big advantages of preemptive threads are better fault tolerance: a single thread failing to yield doesn't stop all other threads from executing. Also normally works better on multi-core machines, since multiple threads execute at once. Finally, you don't have to worry about making sure you're constantly yielding. That can be really annoying inside, e.g., a heavy number crunching loop.

You can mix them, of course. A single preemptive thread can have many cooperative threads running inside it.

Solution 3 - Linux

If you use non-preemptive it does not mean that process doesn't perform context switching when the process is waiting for I/O. The dispatcher will choose another process according to the scheduling model. We have to trust the process.

non-preemptive:

  1. less context switching, less overhead that can be sensible in non-preemptive model

  2. Easier to handle since it can be handled using a single-core processor

preemptive:

Advantage:

  1. In this model, we have a priority that helps us to have more control over the running process

  2. Better concurrency is a bonus

  3. Handling system calls without blocking the entire system

Disadvantage:

  1. Requires more complex algorithms for synchronization and critical section handling is inevitable.

  2. The overhead that comes with it

Solution 4 - Linux

In cooperative (non-preemptive) models, once a thread is given control it continues to run until it explicitly yields control or it blocks.

In a preemptive model, the virtual machine is allowed to step in and hand control from one thread to another at any time. Both models have their advantages and disadvantages.

Java threads are generally preemptive between priorities. A higher priority thread takes precedence over a lower priority thread. If a higher priority thread goes to sleep or blocks, then a lower priority thread can run (assuming one is available and ready to run).

However, as soon as the higher priority thread wakes up or unblocks, it will interrupt the lower priority thread and run until it finishes, blocks again, or is preempted by an even higher priority thread.

The Java Language Specification, occasionally allows the VMs to run lower priority threads instead of a runnable higher priority thread, but in practice this is unusual.

However, nothing in the Java Language Specification specifies what is supposed to happen with equal priority threads. On some systems these threads will be time-sliced and the runtime will allot a certain amount of time to a thread. When that time is up, the runtime preempts the running thread and switches to the next thread with the same priority.

On other systems, a running thread will not be preempted in favor of a thread with the same priority. It will continue to run until it blocks, explicitly yields control, or is preempted by a higher priority thread.

As for the advantages both derobert and pooria have highlighted them quite clearly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlok SaveView Question on Stackoverflow
Solution 1 - LinuxJerry CoffinView Answer on Stackoverflow
Solution 2 - LinuxderobertView Answer on Stackoverflow
Solution 3 - LinuxpooriaView Answer on Stackoverflow
Solution 4 - LinuxAmimo BenjaView Answer on Stackoverflow