Jump to content

Protothread

From Wikipedia, the free encyclopedia

Aprotothreadis a low-overhead mechanism forconcurrent programming.

Protothreads function asstackless,lightweightthreads,orcoroutines,providing a blocking context cheaply using minimal memory per protothread (on the order of single bytes).

Protothreads are used to accomplish anon-preemptedform ofconcurrencyknown ascooperative multitaskingand, therefore, do not incurcontext switchwhen yielding to another thread. Within a protothread, yielding is accomplished by utilizingDuff's devicewithin a thread's function and an external variable used in within theswitch statement.This allows jumping (resuming) from a yield upon another function call. In order toblockthreads, these yields may be guarded by aconditionalso that successive calls to the same function will yield unless the guard conditional is true.

A feature of protothreads relative to other implementations ofcoroutines,or proper threads, is that they are stackless. This has advantages and disadvantages. A disadvantage is that local variables within the protothread cannot be trusted to have retained their values across a yield to another context. They must retain their state through the use of static or external, oftenglobal,variables.[1]An advantage is that they are very lightweight and therefore useful on severely memory constrained systems like small microcontrollers where other solutions are impractical or less desirable.

Tom Duff,ofDuff's devicefame, had this to say about the shortcomings of the method: "a similar trick for interrupt-driven state machines that is too horrible to go into. [...] I never thought it was an adequate general-purpose coroutine implementation because it’s not easy to have multiple simultaneous activations of a coroutine and it’s not possible using this method to have coroutines give up control anywhere but in their top-level routine. A simple assembly-language stack-switching library lets you do both of those."[2]

The protothread concept was developed byAdam Dunkelsand Oliver Schmidt,[3]based on prior work bySimon Tatham[4]andTom Duff.[2]

See also

[edit]

References

[edit]
  1. ^A. Dunkels, O. Schmidt, T. Voigt, and M. Ali,Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems,Proc. ACMSenSys,Boulder, CO, USA, Nov 2006. (PDF,Presentation slides)
  2. ^ab"Brainwagon » Coroutines in C".
  3. ^Adam Dunkels."Protothreads - Lightweight, Stackless Threads in C".Dunkels.RetrievedApril 21,2017.
  4. ^"Coroutines in C".
[edit]