A protothread is a low-overhead mechanism for concurrent programming.
Protothreads are used to accomplish a non-preempted form of concurrency known as cooperative multitasking and, therefore, do not incur context switch when yielding to another thread. Within a protothread, yielding is accomplished by utilizing Duff's device within a thread's function and an external variable used in within the switch statement. This allows jumping (resuming) from a yield upon another function call. In order to block threads, these yields may be guarded by a conditional so that successive calls to the same function will yield unless the guard conditional is true.
The disadvantage of protothreads relative to other implementations of coroutines, or proper threads, is that they are stackless. 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 external, often global, variables.
Tom Duff, of Duff's device fame, 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."
- A. Dunkels, O. Schmidt, T. Voigt, and M. Ali, Protothreads: Simplifying Event-Driven Programming of Memory-Constrained Embedded Systems, Proc. ACM SenSys, Boulder, CO, USA, Nov 2006. (PDF, Presentation slides)
- Adam Dunkels. "Protothreads - Lightweight, Stackless Threads in C". Dunkels.com. Retrieved April 21, 2017.
- Protothread library in C used by Contiki
- Using Protothreads for Sensor Node Programming A PDF with some in-depth discussion about the use of Protothreads
- Protothread library requiring GCC, includes a thread scheduler modeled on the UNIX kernel (a simplified form of POSIX condition variables). This source base also includes a version that supports multiple CPU cores (processors).
- eigenclass.org performed a comparison of protothreads against POSIX threads, and found a 400x improvement in speed  Retrieved from Archive.org Oct 2014
- C++ Protothread
- Symbian C++ Protothread Implementation
|This computer-programming-related article is a stub. You can help Wikipedia by expanding it.|