Skip to content
/ cppco Public

`cppco` is a C++ wrapper library for the cooperative multithreading C library `libco`.

License

Notifications You must be signed in to change notification settings

CzB404/cppco

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cppco

cppco is a C++11 wrapper library for the cooperative multithreading C library libco, originally written by Byuu for higan.

The "cooperative multithreading" libco implements could also be called a coroutine or a fiber implementation.

However, in order to preserve libco's terminology this library will refer to the concept defined by libco as "cothread" and introduces the class co::thread to represent it.

Getting started

cppco is a header only library, so only the include directory needs to be specified as an include path for the compiler, then include the header <co.hpp>.

You also need to use the libco library. This repository defines a CMake file to build and link it.

The simplest "Hello World!" program using cppco is the following:

#include <iostream>
#include <co.hpp>

int main()
{
    using namespace std;

    // Create the cothread.
    auto cothread = co::thread([]()
    {
        // Printing on `cothread`
        cout << "Hello World!" << endl;

        // Switch back to the parent.
        co::active().get_parent().switch_to();
    });

    // Switch to `cothread`.
    cothread.switch_to();

    // Execution will resume here when `cothread` switches back to its parent.
    return 0;
}

Rationale

libco provides a C API which is not safe to use directly in C++ as it doesn't handle C++ features such as destructors or exceptions, leading to resource leaks and crashes unless special care is made to avoid them.

About

`cppco` is a C++ wrapper library for the cooperative multithreading C library `libco`.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published