cross-platform coroutine library of c++
in developing ...
License under the MIT license
Generate document with doxygen.
Doxygen file located at doc/libcopp.doxyfile .
libcopp use cmake to generate makefile and switch build tools.
- [required] GCC or Clang or VC support ISO C++ 98 and upper
- [required] cmake 2.8.9 and upper
- [optional] gtest 1.6.0 and upper (better test supported)
- [required] masm (in vc)
- [optional] if using gtest, pthread is required.
1. make a build directory
mkdir build
2. run cmake command
cmake <libcopp dir> [options]
options can be cmake options. such as set compile toolchains, source directory or options of libcopp that control build actions. libcopp options are listed below:
-DBUILD_SHARED_LIBS=YES|NO [default=NO] enable build dynamic library.
-DLIBCOPP_ENABLE_SEGMENTED_STACKS=YES|NO [default=NO] enable split stack supported context.(it's only availabe in linux and gcc 4.7.0 or upper)
-DGTEST_ROOT=[path] set gtest library install prefix path
3. make libcopp
make [options]
4. run test [optional]
test/coroutine_test
5. install [optional]
make install
Or you can just copy include directory and libcopp.a in lib or lib64 into your project to use it.
Just include headers and linking library file of your platform to use libcopp
There is a simple example of using coroutine manager below:
#include <cstdio>
#include <cstring>
#include <inttypes.h>
#include <stdint.h>
// include manager header file
#include <libcopp/coroutine/coroutine_manager.h>
// define a coroutine runner
class my_runner : public copp::coroutine_manager_runner_base
{
public:
typedef copp::default_coroutine_manager::value_type value_type;
public:
int operator()() {
// ... your code here ...
uint64_t key = get_coroutine_context<copp::default_coroutine_manager::value_type>()->get_key();
printf("cortoutine %" PRIu64 " is running.\n", key);
return 1;
}
};
int main() {
// create a coroutine manager
copp::default_coroutine_manager co_mgr;
typedef copp::default_coroutine_manager::value_type co_type;
// create a coroutine object
co_type* pco = co_mgr.create();
// create and bind runner to coroutine object
pco->create_runner<my_runner>(64 * 1024); // allocate 64KB stack memory
// start a coroutine
co_mgr.start(pco->get_key());
printf("cortoutine %" PRIu64 " exit and return %d.\n", pco->get_key(), pco->get_ret_code());
// destroy a coroutine [optional], all coroutine will be destroyed automatically when co_mgr is destroyed
co_mgr.remove(pco->get_key()); // pco is invalid now.
return 0;
}
And then, you can custom many function such as set your id type, stack allocator, coroutine type and etc. by set your template parameters of coroutine manager.
split stack support: if in Linux and user gcc 4.7.0 or upper, add -DLIBCOPP_ENABLE_SEGMENTED_STACKS=YES to use split stack supported context.