Timeline for answer to How can I create multiple running threads? by Norman Gray
Current License: CC BY-SA 4.0
Post Revisions
7 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 9, 2019 at 12:48 | comment | added | Norman Gray | (correction: protothreads was mentioned, in @sachleen's answer) | |
| Jul 9, 2019 at 10:34 | comment | added | Norman Gray | @EdgarBonet It's a useful distinction, yes. I would regard both this style, and yield-style threads, as simply different styles of cooperative thread, as opposed to preemptive threads, but it's true that they require a different approach to coding them. It would be interesting to see a thoughtful and in-depth comparison of the various approaches mentioned here; one nice library not mentioned above is protothreads. I find things to criticise in both, but also to praise. I (of course) prefer my approach, because it seems most explicit and needs no extra stacks. | |
| Jul 8, 2019 at 18:09 | comment | added | Edgar Bonet |
So yes, those are run-to-completion (RtC) tasks: no task can run before the current one completes its execution by returning from run(). This is in contrast with cooperative threads, that can yield the CPU by, e.g., calling yield() or delay(). Or preemptive threads, that can be scheduled-out at any time. I feel the distinction is important, as I have seen that many people who come around here searching for threads do so because they prefer writing blocking code rather than state machines. Blocking real threads that yield the CPU is fine. Blocking RtC tasks is not.
|
|
| Jul 8, 2019 at 16:43 | history | edited | Norman Gray | CC BY-SA 4.0 |
Slightly expanded the explanation in the light of comments
|
| Jul 8, 2019 at 15:58 | comment | added | Norman Gray |
@EdgarBonet I'm not sure quite what you mean. After the run() method is called, it is not interrupted, so it has the responsibility to finish reasonably promptly. Typically, however, it'll do its work then reschedule itself (possibly automatically, in the case of a subclass of LoopTask) for some future time. A common pattern is for the task to maintain some internal state machine (a trivial example is the light_on_p_ state above) so that it behaves suitably when it is next due.
|
|
| Jul 8, 2019 at 15:48 | comment | added | Edgar Bonet | These are “run-to-completion” tasks, right? | |
| Jul 8, 2019 at 15:41 | history | answered | Norman Gray | CC BY-SA 4.0 |