A protothread is a low-overhead mechanism for
concurrent programming
Concurrent means happening at the same time. Concurrency, concurrent, or concurrence may refer to:
Law
* Concurrence, in jurisprudence, the need to prove both ''actus reus'' and ''mens rea''
* Concurring opinion (also called a "concurrence"), a ...
.
Protothreads function as
stackless, lightweight
threads, or
coroutines
Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, ...
, providing a blocking context cheaply using minimal memory per protothread (on the order of single bytes).
Protothreads are used to accomplish a
non-preempted form of
concurrency known as
cooperative multitasking
Cooperative multitasking, also known as non-preemptive multitasking, is a computer multitasking technique in which the operating system never initiates a context switch from a running Process (computing), process to another process. Instead, in o ...
and, therefore, do not incur
context switch
In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point, and then restoring a different, previously saved, state. This allows multiple processes ...
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
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
Switch statements function ...
. This allows jumping (resuming) from a yield upon another function call. In order to
block
Block or blocked may refer to:
Arts, entertainment and media Broadcasting
* Block programming, the result of a programming strategy in broadcasting
* W242BX, a radio station licensed to Greenville, South Carolina, United States known as ''96.3 ...
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.
A feature of protothreads relative to other implementations of
coroutines
Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, ...
, 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, often
global
Global may refer to:
General
*Globe, a spherical model of celestial bodies
*Earth, the third planet from the Sun
Entertainment
* ''Global'' (Paul van Dyk album), 2003
* ''Global'' (Bunji Garlin album), 2007
* ''Global'' (Humanoid album), 198 ...
, variables. 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, 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."
The protothread concept was developed by
Adam Dunkels
Adam Dunkels (born 28 May 1978) is a Swedish computer scientist, software engineer, entrepreneur, and founder of Thingsquare, an Internet of things (IoT) product development business.
His father was professor of mathematics Andrejs Dunkels. His ...
and Oliver Schmidt, based on prior work by
Simon Tatham
Simon Tatham (born 3 May 1977) is a British computer programmer. He created and maintains PuTTY, a free software implementation of Secure Shell (SSH) and Telnet for Microsoft Windows and Unix, along with an xterm terminal emulator. He is also ...
and
Tom Duff.
See also
*
Coroutine
Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative task ...
*
Microthread
Microthreads are functions that may run in parallel to gain increased performance in microprocessors. They provide an execution model that uses a few additional instructions in a conventional processor to break code down into fragments that execu ...
References
External links
Protothread library in Cused by
Contiki
Contiki is an operating system for networked, memory-constrained systems with a focus on low-power wireless Internet of Things (IoT) devices. Contiki is used for systems for street lighting, sound monitoring for smart cities, radiation monitori ...
Using Protothreads for Sensor Node ProgrammingA PDF with some in-depth discussion about the use of Protothreads
Protothread libraryrequiring
GCC, includes a
thread scheduler modeled on the
UNIX kernel (a simplified form of
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
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 spee
Retrieved from Archive.org Oct 2014
C++ ProtothreadSymbian C++ Protothread Implementation
Threads (computing)
{{compu-prog-stub