yyzybb537 / libgo

Go-style concurrency in C++11
MIT License
3.18k stars 754 forks source link
c-plus-plus coroutine libgo

libgo

Build Status

libgo -- a coroutine library and a parallel Programming Library

Libgo is a stackful coroutine library for collaborative scheduling written in C++ 11, and it is also a powerful and easy-to-use parallel programming library.

Three platforms are currently supported:

Linux

MacOSX

Windows (Win7 or above,x86 or x64,complie with VS2015/2017)

Using libgo to write multi-threaded programs, it can be developed as fast and logical as golang and Erlang concurrent languages, and has the performance advantages of C++ native.It make it happen that one can serve God and Mammon.

Libgo has the following characteristics:

compile and use libgo :

If you have installed vcpkg, you can install it directly using vcpkg: $ vcpkg install libgo

performance

Like golang, libgo implements a complete scheduler (users only need to create a coroutine without concern for the execution, suspension and resource recovery of the coroutine). Therefore, libgo is qualified to compare the performance of single-threaded with golang (It is not qualified to do performance comparison in different ability).

Test environment: 2018 13-inch MAC notebook (CPU minimum) Operating System: Mac OSX CPU: 2.3 GHz Intel Core i5 (4 Core 8 Threads) Test script: $test/golang/test.sh thread_number

Matters needing attention(WARNING):

TLS or non-reentrant library functions that depend on TLS implementation should be avoided as far as possible. If it is unavoidable to use, we should pay attention to stop accessing the TLS data generated before handover after the process handover.

There are several kinds of behaviors that may cause the process switching:

System Call List of Hook on Linux System:

    connect   
    read      
    readv     
    recv      
    recvfrom  
    recvmsg   
    write     
    writev    
    send      
    sendto    
    sendmsg   
    poll      
    __poll
    select    
    accept    
    sleep     
    usleep    
    nanosleep
    gethostbyname                                                               
    gethostbyname2                                                              
    gethostbyname_r                                                             
    gethostbyname2_r                                                            
    gethostbyaddr                                                               
    gethostbyaddr_r

The above system calls are all possible blocking system calls. The whole thread is no longer blocked in the process. During the blocking waiting period, the CPU can switch to other processes to execute.System calls executed in native threads by HOOK are 100% consistent with the behavior of the original system calls without any change.

    socket
    socketpair
    pipe
    pipe2
    close     
    __close
    fcntl     
    ioctl     
    getsockopt
    setsockopt
    dup       
    dup2      
    dup3      

The above system calls will not cause blocking, although they are also Hook, but will not completely change their behavior, only for tracking socket options and status.

System Call List of Hook on Windows System:

    ioctlsocket                                                                        
    WSAIoctl                                                                           
    select                                                                             
    connect                                                                            
    WSAConnect                                                                         
    accept                                                                             
    WSAAccept                                                                          
    WSARecv                                                                            
    recv                                                                               
    recvfrom                                                                           
    WSARecvFrom                                                                        
    WSARecvMsg                                                                         
    WSASend                                                                            
    send                                                                               
    sendto                                                                             
    WSASendTo                                                                          
    WSASendMsg