简体   繁体   English

在执行过程中睡一个线程

[英]sleeping a thread in the middle of execution

What happens when a thread is put to sleep by other thread, possible by main thread, in the middle of its execution? 当一个线程被其他线程(可能是主线程)在执行过程中进入sleep时会发生什么?

assuming I've a function Producer . 假设我有一个函数Producer What if Consumer sleep() s the Producer in the middle of production of one unit ? 如果Consumer在一个单位的生产过程中sleep()生产者怎么办?

Suppose the unit is half produced. 假设该单位是一半生产。 and then its put on sleep(). 然后它就睡觉了()。 The integrity of system may be in a problem 系统的完整性可能存在问题

The thread that sleep is invoked on is put in the idle queue by the thread scheduler and is context switched out of the CPU it is running on, so other threads can take it's place. 调用sleep的线程由线程调度程序放入空闲队列,并且是从运行它的CPU中切换出的上下文,因此其他线程可以取而代之。

All context (registers, stack pointer, base pointer, etc) are saved on the thread stack, so when it's run next time, it can continue from where it left off. 所有上下文(寄存器,堆栈指针,基指针等)都保存在线程堆栈中,因此当它下次运行时,它可以从它停止的位置继续。

The OS is constantly doing context switches between threads in order to make your system seem like it's doing multiple things. 操作系统不断在线程之间进行上下文切换,以使您的系统看起来像是在做多件事。 The OS thread scheduler algorithm takes care of that. OS线程调度程序算法负责这一点。

Thread scheduling and threading is a big subject, if you want to really understand it, I suggest you start reading up on it. 线程调度和线程是一个很大的主题,如果你想真正理解它,我建议你开始阅读它。 :) :)

EDIT: Using sleep for thread synchronization purposes not advised, you should use proper synchronization mechanisms to tell the thread to wait for other threads, etc. 编辑:不建议使用sleep进行线程同步,你应该使用适当的同步机制告诉线程等待其他线程等。

It would be unadvisable for one thread to forcibly and synchronously interfere with the execution of another thread. 一个线程强制并同步地干扰另一个线程的执行是不可取的。 One thread could send an asynchronous message to another requesting that it reschedule itself in some way, but that would be handled by the other thread when it was in a suitable state to do so. 一个线程可以向另一个线程发送异步消息,请求它以某种方式重新安排自己,但是当它处于合适的状态时,将由另一个线程处理。

Assuming they communicate using channels that are thread-safe, nothing bad shoudl happen, as the sleeping thread will wake up eventually and grab data from its task queue or see that some semaphore has been set and read the prodced data. 假设他们使用线程安全的通道进行通信,不会发生任何坏事,因为睡眠线程最终将被唤醒并从其任务队列中获取数据或者看到已设置了一些信号量并读取了所生成的数据。

If the threads communicate using nonvolatile variables or direct function calls that change state, that's when Bad Things occur. 如果线程使用非易失性变量或直接函数调用进行通信来改变状态,那就是Bad Things发生的时候。

There is no problem associated with this, unless some state is mutated while the thread sleeps, so it wakes up with a different set of values than before going to sleep. 没有任何问题与此相关,除非某些状态在线程休眠时发生变异,因此它会使用与进入休眠之前不同的一组值唤醒。

Threads are switched in and out of execution by the CPU all the time, but that does not affect the overall outcome of their execution, assuming no data races or other bugs are present. 线程一直由CPU切换和执行,但这并不影响其执行的总体结果,假设不存在数据争用或其他错误。

I don't know of a way for a thread to forcibly cause another thread to sleep. 我不知道线程强行导致另一个线程休眠的方法。 If two threads are accessing a shared resource (like an input/output queue, which seems likely for you Produce/Consumer example), then both threads may contend for the same lock. 如果两个线程正在访问共享资源(如输入/输出队列,这似乎可能是您的Produce / Consumer示例),则两个线程都可以争用同一个锁。 The losing thread must wait for the other thread to release the lock if the contention is not the "trylock" variety. 如果争用不是“trylock”变种,则丢失的线程必须等待另一个线程释放锁。 The thread that waits is placed into a waiting queue associated with the lock, and is removed from the schedulers run queue. 等待的线程被放入与锁相关联的等待队列中,并从调度程序运行队列中删除。 When the winning thread releases the lock, the code checks the queue to see if there are threads still waiting to acquire it. 当获胜线程释放锁时,代码会检查队列以查看是否还有线程仍在等待获取它。 If there are, one is chosen as the winner and is given the lock, and placed in the scheduler run queue. 如果有,则选择一个作为获胜者并获得锁定,并将其置于调度程序运行队列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM