mirror of
https://github.com/boostorg/coroutine.git
synced 2026-01-30 07:42:52 +00:00
c++11 examples for symmetric coroutines
This commit is contained in:
39
example/cpp11/symmetric/Jamfile.v2
Normal file
39
example/cpp11/symmetric/Jamfile.v2
Normal file
@@ -0,0 +1,39 @@
|
||||
# Boost.Coroutine Library Examples Jamfile
|
||||
|
||||
# Copyright Oliver Kowalke 2009.
|
||||
# Distributed under the Boost Software License, Version 1.0.
|
||||
# (See accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
# For more information, see http://www.boost.org/
|
||||
|
||||
import common ;
|
||||
import feature ;
|
||||
import indirect ;
|
||||
import modules ;
|
||||
import os ;
|
||||
import toolset ;
|
||||
|
||||
project boost/coroutine/example
|
||||
: requirements
|
||||
<library>../../../build//boost_coroutine
|
||||
<library>/boost/program_options//boost_program_options
|
||||
<library>/boost/random//boost_random
|
||||
<library>/boost/thread//boost_thread
|
||||
<toolset>gcc-4.7,<segmented-stacks>on:<cxxflags>-fsplit-stack
|
||||
<toolset>gcc-4.7,<segmented-stacks>on:<cxxflags>-DBOOST_USE_SEGMENTED_STACKS
|
||||
<toolset>gcc-4.8,<segmented-stacks>on:<cxxflags>-fsplit-stack
|
||||
<toolset>gcc-4.8,<segmented-stacks>on:<cxxflags>-DBOOST_USE_SEGMENTED_STACKS
|
||||
<toolset>gcc-4.9,<segmented-stacks>on:<cxxflags>-fsplit-stack
|
||||
<toolset>gcc-4.9,<segmented-stacks>on:<cxxflags>-DBOOST_USE_SEGMENTED_STACKS
|
||||
<link>static
|
||||
<threading>multi
|
||||
;
|
||||
|
||||
exe simple
|
||||
: simple.cpp
|
||||
;
|
||||
|
||||
exe merge_arrays
|
||||
: merge_arrays.cpp
|
||||
;
|
||||
77
example/cpp11/symmetric/merge_arrays.cpp
Normal file
77
example/cpp11/symmetric/merge_arrays.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
|
||||
// Copyright Keld Helsgaun 2000, Oliver Kowalke 2014.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstddef>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include <boost/coroutine/all.hpp>
|
||||
|
||||
typedef boost::coroutines::symmetric_coroutine< void > coro_t;
|
||||
|
||||
std::vector< int > merge( std::vector< int > const& a, std::vector< int > const& b)
|
||||
{
|
||||
std::vector< int > c;
|
||||
std::size_t idx_a = 0, idx_b = 0;
|
||||
coro_t * other_a = 0, * other_b = 0;
|
||||
|
||||
coro_t coro_a(
|
||||
[&]( coro_t::self_type & self) {
|
||||
while ( idx_a < a.size() )
|
||||
{
|
||||
if ( b[idx_b] < a[idx_a])
|
||||
self( * other_b);
|
||||
c.push_back(a[idx_a++]);
|
||||
}
|
||||
while ( c.size() < a.size() + b.size())
|
||||
c.push_back( b[idx_b]);
|
||||
});
|
||||
|
||||
coro_t coro_b(
|
||||
[&]( coro_t::self_type & self) {
|
||||
while ( idx_b < b.size() )
|
||||
{
|
||||
if ( a[idx_a] < b[idx_b])
|
||||
self( * other_a);
|
||||
c.push_back(b[idx_b++]);
|
||||
}
|
||||
while ( c.size() < ( a.size() + b.size() ) )
|
||||
c.push_back( a[idx_a]);
|
||||
});
|
||||
|
||||
|
||||
other_a = & coro_a;
|
||||
other_b = & coro_b;
|
||||
|
||||
coro_a();
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void print( std::string const& name, std::vector< int > const& v)
|
||||
{
|
||||
std::cout << name << " : ";
|
||||
for ( auto itm : v)
|
||||
{ std::cout << itm << " "; }
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
int main( int argc, char * argv[])
|
||||
{
|
||||
std::vector< int > a = { 1, 5, 6, 10 };
|
||||
print( "a", a);
|
||||
|
||||
std::vector< int > b = { 2, 4, 7, 8, 9, 13 };
|
||||
print( "b", b);
|
||||
|
||||
std::vector< int > c = merge( a, b);
|
||||
print( "c", c);
|
||||
|
||||
std::cout << "Done" << std::endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
41
example/cpp11/symmetric/simple.cpp
Normal file
41
example/cpp11/symmetric/simple.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
// Copyright Oliver Kowalke 2009.
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include <boost/coroutine/all.hpp>
|
||||
|
||||
typedef boost::coroutines::symmetric_coroutine< void > coro_t;
|
||||
|
||||
coro_t * c1 = 0;
|
||||
coro_t * c2 = 0;
|
||||
|
||||
int main( int argc, char * argv[])
|
||||
{
|
||||
coro_t coro1(
|
||||
[&]( coro_t::self_type & self) {
|
||||
std::cout << "foo1" << std::endl;
|
||||
self( * c2);
|
||||
std::cout << "foo2" << std::endl;
|
||||
self( * c2);
|
||||
std::cout << "foo3" << std::endl;
|
||||
});
|
||||
coro_t coro2(
|
||||
[&]( coro_t::self_type & self) {
|
||||
std::cout << "bar1" << std::endl;
|
||||
self( * c1);
|
||||
std::cout << "bar2" << std::endl;
|
||||
self( * c1);
|
||||
std::cout << "bar3" << std::endl;
|
||||
});
|
||||
c1 = & coro1;
|
||||
c2 = & coro2;
|
||||
coro1();
|
||||
std::cout << "Done" << std::endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Reference in New Issue
Block a user