抢占线程Vs非抢占线程

时间:2022-12-07 21:04:16

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. 解释理解是否正确。
  3. Explain the advantages and disadvantages of both models.
  4. 解释这两种模型的优缺点。
  5. An example of when to use what will be really helpful.
  6. 一个什么时候使用真正有用的例子。
  7. 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?
  8. 如果我在Linux中创建了一个线程(system v或Pthread),而没有提到任何选项(是否存在?),默认的线程模型是抢占线程模型?

3 个解决方案

#1


29  

  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. 不,你的理解并不完全正确。非抢占式(也称为合作)线程通常会手动地控制让其他线程在它们完成之前运行(尽管这取决于该线程调用yield()(或其他什么)来实现这一点。
  3. Preempting threading is simpler. Cooperative threads have less overhead.
  4. 线程抢占所就更简单了。协作线程的开销更小。
  5. 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.
  6. 通常使用先发制人的。如果您发现您的设计有很多线程交换开销,那么合作线程可能是一种优化。在许多(大多数)情况下,这将是一笔相当大的投资,但收益甚微。
  7. 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...
  8. 是的,默认情况下您将获得抢占线程,但是如果您查看CThreads包,它支持协作线程。很少有人(现在)想要合作的线程,我不确定它是否在过去的十年里被更新过……

#2


15  

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.

非抢占线程也称为合作线程。这方面的一个例子是POE (Perl)。另一个例子是经典的Mac OS(在OS X之前),合作线程在放弃CPU之前,一直独占使用CPU。然后调度程序选择另一个要运行的线程。

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.

抢占线程可以像协作线程一样自动放弃CPU,但如果不这样做,它就会从线程中取出,调度程序将启动另一个线程。POSIX和SysV线程属于这一类。

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.

当然,你可以混合它们。一个先发制人的线程可以在其中运行许多协作线程。

#3


6  

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

如果使用非抢占性,并不意味着进程在等待I/O时不做上下文切换。调度程序将根据调度模型选择另一个进程。在这个模型中,我们必须相信这个过程。

non-preemptive:

无优先的:策略

1.less context switch = less over head that can be sensible in non-preemptive model

1。更少的上下文切换=在非抢占模型中更少的头部

2.It easier to handle because it can be handled on single-core processor

2。它更容易处理,因为它可以在单核处理器上处理

preemptive:

先发制人的:

Advantage:

优势:

1.In this model we have priority that can help us to have more control on running process

1。在这个模型中,我们有优先级,可以帮助我们对运行过程有更多的控制。

2.We can see the better concurrency

2。我们可以看到更好的并发性

3.we can handle a system call without blocking entire system

3所示。我们可以在不阻塞整个系统的情况下处理系统调用。

Disadvantage:

劣势:

1.We need complex algorithm for locking and we have critical section problem that should be handled

1。我们需要复杂的算法来锁定,我们有关键的部分问题需要处理

2.Often a big overhead that we should pay

2。我们经常要支付一大笔开销

#1


29  

  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. 不,你的理解并不完全正确。非抢占式(也称为合作)线程通常会手动地控制让其他线程在它们完成之前运行(尽管这取决于该线程调用yield()(或其他什么)来实现这一点。
  3. Preempting threading is simpler. Cooperative threads have less overhead.
  4. 线程抢占所就更简单了。协作线程的开销更小。
  5. 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.
  6. 通常使用先发制人的。如果您发现您的设计有很多线程交换开销,那么合作线程可能是一种优化。在许多(大多数)情况下,这将是一笔相当大的投资,但收益甚微。
  7. 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...
  8. 是的,默认情况下您将获得抢占线程,但是如果您查看CThreads包,它支持协作线程。很少有人(现在)想要合作的线程,我不确定它是否在过去的十年里被更新过……

#2


15  

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.

非抢占线程也称为合作线程。这方面的一个例子是POE (Perl)。另一个例子是经典的Mac OS(在OS X之前),合作线程在放弃CPU之前,一直独占使用CPU。然后调度程序选择另一个要运行的线程。

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.

抢占线程可以像协作线程一样自动放弃CPU,但如果不这样做,它就会从线程中取出,调度程序将启动另一个线程。POSIX和SysV线程属于这一类。

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.

当然,你可以混合它们。一个先发制人的线程可以在其中运行许多协作线程。

#3


6  

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

如果使用非抢占性,并不意味着进程在等待I/O时不做上下文切换。调度程序将根据调度模型选择另一个进程。在这个模型中,我们必须相信这个过程。

non-preemptive:

无优先的:策略

1.less context switch = less over head that can be sensible in non-preemptive model

1。更少的上下文切换=在非抢占模型中更少的头部

2.It easier to handle because it can be handled on single-core processor

2。它更容易处理,因为它可以在单核处理器上处理

preemptive:

先发制人的:

Advantage:

优势:

1.In this model we have priority that can help us to have more control on running process

1。在这个模型中,我们有优先级,可以帮助我们对运行过程有更多的控制。

2.We can see the better concurrency

2。我们可以看到更好的并发性

3.we can handle a system call without blocking entire system

3所示。我们可以在不阻塞整个系统的情况下处理系统调用。

Disadvantage:

劣势:

1.We need complex algorithm for locking and we have critical section problem that should be handled

1。我们需要复杂的算法来锁定,我们有关键的部分问题需要处理

2.Often a big overhead that we should pay

2。我们经常要支付一大笔开销