Thread priorities in Lua

最后都变了- 提交于 2019-12-23 18:14:57

问题


I had a look at the Lua book and learned that multi-threading in Lua is cooperative. What I couldn't find is some information about thread priorities.I guess that threads with the same priority run till completion, since multi-threading is cooperative, or a yield is done. What about a thread that has higher priority than another one?

Is it able to interrupt the one with lower priority or will it run next when the thread with lower priority has run till completion?


回答1:


There are no native threads (preemptive multitasking) in Lua, there is however cooperative multitasking as you said.

The difference between preemptive and cooperative multitasking, is that in preemptive multitasking the "threads" are not necessarily allowed to run until completion, but can be preempted by other threads. This is done by the scheduler, which runs at regular intervals, switching one thread for another. This is where priorities come in. If a thread with higher priority wants to run, it can preempt an already running thread with lower priority, and the scheduler will chose that thread (depending on the scheduling strategy), next time the scheduler runs.

In cooperative multitasking there does not have to be a scheduler (though for practical reasons its usually a good idea to have one). There are however co-processes. A co-process is like a thread, except it can not be preempted. It can either run to completion, or yield to another co-process and allow that to run.

So back to your question, if you want priorities with cooperative multitasking, you need to write a scheduler, which decides which co-process to run, given its priority, and you need to write your co-process, so they give up processing once in a while, and turn back control to the scheduler.

Edit

To clarify, there is a slight difference between non-preemptive multitasking and cooperative multitasking. Non-preemptive multitasking is a bit broader, as it allows both static scheduling and cooperative multitasking.

Static scheduling means that a scheduler can schedule periodic tasks, which can then run when a task yields, maybe with a higher priority.

Cooperative multitasking is also a type of non-preemptive multitasking. However, here tasks are only scheduled by the tasks themselves, and control is explicitly yielded from on task to another, but which task it yields to, can be based on a priority.




回答2:


In Lua threads cannot run in paralel (ie on multiple cores) within one Lua state. There's no concurrency, since it's cooperative multitasking. Only when one thread suspends execution (yields), can another thread resume. At no point can two Lua threads execute concurrently within one Lua state.

What you're talking about is preemption - a scheduler interrupting one thread to let another one execute.



来源:https://stackoverflow.com/questions/15943785/thread-priorities-in-lua

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!