Windows Install
Ubuntu Install C++
Ubuntu Install
coroutine
With Boost.Coroutine it is possible to use coroutines in C++. Coroutines are a feature of other programming languages, which often use the keyword yield
for coroutines. In these programming languages, yield
can be used like return
. However, when yield
is used, the function remembers the location, and if the function is called again, execution continues from that location.
#include <boost/coroutine/all.hpp>
#include <iostream>
using namespace boost::coroutines;
void cooperative(coroutine<void>::push_type &sink)
{
std::cout << "Hello";
sink();
std::cout << "world";
}
int main()
{
coroutine<void>::pull_type source{cooperative};
std::cout << ", ";
source();
std::cout << "!\n";
}
using namespace boost::coroutines;
void cooperative(coroutine<int>::push_type &sink)
{
int i=0;
sink(++i);
sink(++i);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
coroutine<int>::pull_type source{cooperative};
qDebug()<<source.get();
if(source){
qDebug()<<source().get();
}
return a.exec();
}
//1 2
#include <boost/coroutine/all.hpp>
#include <stdexcept>
#include <iostream>
using boost::coroutines::coroutine;
void cooperative(coroutine<void>::push_type &sink)
{
sink();
throw std::runtime_error("error");
}
int main()
{
coroutine<void>::pull_type source{cooperative};
try
{
source();
}
catch (const std::runtime_error &e)
{
std::cerr << e.what() << '\n';
}
}