The difference between the process and multithreading

Source: Internet
Author: User

Reproduced:

----------------------------------------------------------------------First Article------------------------------------------------ -------------------

Coroutine and real thread of the difference is quite a lot, but it is not good to say is the limit, notice the use of Contiki environment
is the sensor network node, in general, mostly memory is extremely limited, in contrast, CPU resources can be less
Limited (or so, the CPU is working longer only to affect the life of the node, and the RAM is not enough) the node simply
Impossible to work up).

Protothread is in order to solve the problem that multithreading will consume too much RAM and increase CPU processing to swap.

Each function (Protothread) is fully returned at yield, which is equivalent to multiple threads sharing the same
Stack. But the difference between this and the event-based system (like TinyOS) is that when the system is programmed you still
Can be thought of as sequential execution (after yield if you continue to execute, not from the beginning of the function but from the last yield
The event-based system must be used to split the system into many event (simple comprehension, you can
In many cases, it is not a very good way for a person to think that the program is written in the form of a state machine.
Model, for example, we will be very accustomed to do this time what to do, the condition is not mature to continue to do what;
form, you have to divide yourself into several states to make a state transfer diagram, and use this thread-like Protothread
You do not need to explicitly show your state machine).

As for the limitations of Protothread (Coroutine and its nature), depending on the implementation, if a portable switch is used,
So the limit is too much, can not use switch, if you use the GNU computed Goto to achieve, there is no such restriction;
However, whatever the implementation, it is important to note that if you want the auto variable of the function to hold the last value, it must be declared as
Static, this is the essential limitation of implementing the Coroutine class mechanism in C language.

These things, when it comes to the end, are a continuation concept, but they're not used outside of the functional programming community.
Concept only.

The above talk about Protothread may be too general, if you want to know the details, you should read this paper:
Protothreads:simplifying Event-driven Programming of memory-constrained Embedded Systems
Adam dunkels, Oliver Schmidt, Thiemo Voigt, Muneeb Ali

The

Protothread or coroutine, is mainly to provide continuation point. That is, after the function returns from yield, the next call is followed by the next sentence of Yeild. One of the goals is to simplify the code structure and enhance readability. Of course, such things as iterator can be more convenient to achieve. The

Coroutine can be a self-contained stack (a coordinated multitasking usage of thread), or it can be a shared stack. A shared stack is an implementation such as Freertos/coroutine,protothread. The advantage is that memory is saved; The biggest disadvantage is the shared stack, so all stack auto variables are lost in yield. The
workaround is either to use the static/global variable, not to save on the stack, but may lose the reentrant feature (whether or not to lose sight of the implementation), or use a heap like malloc, but there is a memory leak (typically a yield reset), a memory fragmentation problem (malloc itself, determined by its algorithm). The

implementation means, compliant, portable, is a switch-based implementation. In order for the coroutine to be usable and useful, at least depending on the C standard, or the C standard is not said, but after discussion, is a legitimate description of the situation: after the
-  macro expands, the code in the macro is on a physical line. The case of the
- switch can be nested with statements such as for/if. Switch (BBB)  { for (XX; XX; XX)  { case 1: ... ; case 2:  YYY} } is legal.
The second is the core of the switch implementation and, if there is a change, over. The disadvantage of the

Standard switch implementation:
- switch must be used with caution: there is no yield in the middle of the embedded switch, or a return point (Return,exit see Implementation). This can be avoided with if/else.
- switch back to the Continuation point call once the extra overhead is relatively large, one to compare the past. The

uses embedded assembly, the GNU Label,computed goto and other implementations, the limit is less, the efficiency is also high. One of the problems is portable; the other is whether the compiler optimizer is causing the problem, the embedded assembly this is obviously, GNU I have not used, did not understand.

----------------------------------------------------------------------First Article------------------------------------------------ -------------------

The difference between the process and multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.