Lightweight co-Libraries C language implementation

Source: Internet
Author: User

Imitation of the cloud-wind libraries interface design, I spent an afternoon and evening to reconstruct the previously written libraries, the interface is now the same as the cloud wind greatly the interface of the coprocessor, are imitation of Lua asymmetric process. We still do not use the Ucontext.h component (with ucontext.h components to achieve the association of articles overwhelming, you can find yourself, with longjmp implementation of a lot less, with inline assembly to achieve less), our co-Libraries can run on the operating system compatible X86 platform, various unix-like operating systems , the Windows operating system can be, but you have to use GCC or clang or compatible with the MinGW compilation tool to compile a 32-bit program to run. When running a thread in a multithreaded environment, different threads do not share the group, that is, any two threads, if they belong to a different thread, they must belong to a different group of collaborators. If we want to use the respective APIs of the operating system, such as shared memory, to achieve the co-communication between different threads, we do not implement a channel like Goroutine. Co-Libraries for the shared stack mode, a co-process group can hold up to 1 million co-processes, each co-process 128Kbytes stack space, I use the top command to monitor the running of 1 million test program, the test program memory consumption peak of about 280M, It is possible to calculate the peak memory consumption of each coprocessor at around 280bytes. It can be inferred that if we run 10 million co-processes, we need at least 10 process groups, each of the 280M, 2800M = 2.7G = 4G Total theoretical space-1G kernel space, so our co-Libraries can support the maximum number of threads is 10 million, which is theoretically limited. With the GPROF test program performance, 2,999,997 times the co-switch shared 0.39 seconds, each switching time around 130ns. Most importantly, all of our libraries's source code adds up to about 400 lines, which also includes trace notes and header files. If it can be ported to the X64 version, then our co-Libraries can be used in real-world development, and is no longer a mere teaching-only gadget.

Project GitHub Link: https://github.com/Yuandong-Chen/coroutine

Lightweight co-Libraries C language implementation

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.