• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Free course: C++ 11 to 20: Boost Coroutine2. What Why and How. Learn Boost Coroutine asynchronous programming by example.

ptf

Joined
3/29/21
Messages
20
Points
13
C++ Boost Coroutine2. What Why and How. Learn Boost Coroutine asynchronous programming by example.



C++:
// Youtube video: https://youtu.be/omEWXWUg5FA
#include <boost/coroutine2/all.hpp>
#include <iostream>
#include <vector>

using namespace boost::coroutines2;

void co_push_fun1(coroutine<int>::push_type& yield, int a) {
    for (int i = 0; i < a; i++) {
        // logic before yield
        std::cout << "coroutine pushing: " << i << std::endl;
        yield(i);
        // logic after yield
        std::cout << "coroutine pushed: " << i << std::endl;
    }
}

void co_pull_fun1(coroutine<int>::pull_type& yield) {
    int round = 0;
    while (yield) {
        // logic before yield
        std::cout << "coroutine pulled: " << round << std::endl;
        auto i = yield.get();
        std::cout << "coroutine pulled value: " << i << std::endl;
        yield();
        // logic after yield
        std::cout << "coroutine pulling: " << round << std::endl;
        round++;
    }
}

void test_co_pull_func1() {
    coroutine<int>::pull_type resume{std::bind(co_push_fun1, std::placeholders::_1, 6)};
    while (resume) {
        std::cout << "caller while: " << resume.get() << std::endl;
        resume();
    }
    coroutine<int>::pull_type resume2{std::bind(co_push_fun1, std::placeholders::_1, 3)};
    for (int c : resume2) {
        std::cout << "caller for: " << c << std::endl;
    }
}

void test_co_push_func1() {
    coroutine<int>::push_type sink{co_pull_fun1};
    std::vector<int> v{1, 1, 2, 3};
    for (int i : v) {
        std::cout << "caller sinking: " << i << std::endl;
        sink(i);
        std::cout << "caller sinked: " << i << std::endl;
    }
}

int main() {
    std::cout << "=============" << std::endl;
    test_co_pull_func1();
    std::cout << "=============" << std::endl;
    test_co_push_func1();
}
 
It's not marketing(What am I getting?). It's purely knowledge sharing.
 
Back
Top