본문 바로가기

프로그램

Protothread

출처 : http://www.sics.se/~adam/pt


Protothreads are a extremely lightweight, stackless threads that provides a blocking context on top of an event-driven system, without the overhead of per-thread stacks. The purpose of protothreads is to implement sequential flow of control without using complex state machines or full multi-threading. Protothreads provides conditional blocking inside a C function.


프로토스레드는 매우 가볍고 스택이 없는 스레드라 할 수 있는데, 스택이 없는 스레드란 스레드마다 스택을 가지게 됨으로써 발생하는 오버헤드없이 이벤트-드리븐 시스템의 상위에서 작업의 진행을 중지시킬 수 있는 기능을 제공하는 것을 말한다. 프로토스레드의 목적은 복잡한 상태 머신을 이용하거나 멀티-스레드를 이용하지 않고 순차적 실행 흐름을 제어하는 구현을 할 수 있도록 하는 것이다.

In memory constrained systems, such as deeply embedded systems, traditional multi-threading may have a too large memory overhead. In traditional multi-threading, each thread requires its own stack, that typically is over-provisioned. The stacks may use large parts of the available memory.

메모리가 제한적인 시스템으로 경량의 임베디드 시스템을 들 수 있는데, 이제까지의 멀티 스레드 방식은 매우 큰 메모리 오버헤드를 가질 수 있다. 이제까지의 멀티 스레드 방법은 각 스레드마다 자신의 스택을 가져야 하고, 이것은 보통, 시스템의 한계를 넘어서는 경우가 될 수 있다. 그 스택은 사용가능한 메모리의 너무 많은 부분을 사용해야한다.

The main advantage of protothreads over ordinary threads is that protothreads are very lightweight: a protothread does not require its own stack. Rather, all protothreads run on the same stack and context switching is done by stack rewinding. This is advantageous in memory constrained systems, where a stack for a thread might use a large part of the available memory. A protothread only requires only two bytes of memory per protothread. Moreover, protothreads are implemented in pure C and do not require any machine-specific assembler code.

보통의 스레드에 비하여 프로토스레드의 가장 중요한 장점으로는 프로토 스레드가 매우 작다는 것이다: 하나의 프로토스레드는 스스로를 위한 스택을 가질 필요는 없다. 모든 프로토스레드는 같은 스택에서 동작하며, 작업의 전환을 스택을 되감는 방법을 통하여 수행된다. 이것은 메모리의 제약이 있는 시스템에서 스택이 사용가능한 메모리의 너무 많은 부분을 사용해할 경우 장점으로 작용한다. 하나의 프로토스레드는 프로토스레드 하나당 2바이트의 메모리만을 필요로한다. 그리고 프로토스레드는 순수하게 C를 이용하여 구현할 수 있으며 동작하는 플랫폼에 특수한 어셈블러 코드를 작성할  필요없이 구현할 수 있다.

A protothread runs within a single C function and cannot span over other functions. A protothread may call normal C functions, but cannot block inside a called function. Blocking inside nested function calls is instead made by spawning a separate protothread for each potentially blocking function. The advantage of this approach is that blocking is explicit: the programmer knows exactly which functions that may block that which functions that are not able block.

하나의 프로토스레드는 하나의 C 함수에서 동작하고 다른 함수에 그 영향을 미칠 수 없다. 하나의 프로토스레드는 일반적인 C함수에서 호출될 수 있으나, 그 함수가 호출한 다른 함수의 내부의 작업은 멈출 수 없다.  함수 내에서 호출된 함수의 작업을 멈추도록 하는 작업은 각 함수의 프로토스레드에 의하여 만들어진다.(의역 해보자면 함수 바깥에서는 그 함수의 동작을 멈출 수 있도록 하는 방법은 그 함수 내부에 프로토스레드의 동작을 멈추도록 하는 코드 없이는 불가능하다는 이야기임). 이러한 접근의 장점은 작업을 멈추도록 하는 것이 명백해진다는 것이다: 프로그래머는 어떠한 함수가 작업을 멈출 수 있고 어떠한 함수가 작업을 멈출 수 없는지 명확하게 알 수 있다.

Protothreads are similar to asymmetric co-routines. The main difference is that co-routines uses a separate stack for each co-routine, whereas protothreads are stackless. The most similar mechanism to protothreads are Python generators. These are also stackless constructs, but have a different purpose. Protothreads provides blocking contexts.

프로토스레드는 불균형 협력 루틴과 비슷하다. 중요한 차이점은 협력 루틴은 각 협력 루틴마다 나누어진 스택을 이용하지만 반면에 프로토스레드는 스택이 없다는 것이다. 프로토스레드와 가장 비슷한 메커니즘으로는 파이썬 언어의 제너레이터이다. 이것 역시 스택이 없는 구조이지만, 다른 목적을 가지고 있다. 프로토스레드는 작업의 중지를 제공한다.

'프로그램' 카테고리의 다른 글

12주차  (0) 2009.05.26
Using Multi-Threading in Contiki  (0) 2009.05.22
11주차  (0) 2009.05.20
10주차  (2) 2009.05.13
2주차 PPT  (0) 2009.03.10