Imported Upstream version 1.64.0
[platform/upstream/boost.git] / libs / coroutine2 / example / segmented.cpp
1
2 //          Copyright Oliver Kowalke 2014.
3 // Distributed under the Boost Software License, Version 1.0.
4 //    (See accompanying file LICENSE_1_0.txt or copy at
5 //          http://www.boost.org/LICENSE_1_0.txt)
6
7 #include <cstdlib>
8 #include <iostream>
9
10 #include <boost/config.hpp>
11
12 #include <boost/coroutine2/all.hpp>
13
14 #ifdef BOOST_MSVC //MS VisualStudio
15 __declspec(noinline) void access( char *buf);
16 #else // GCC
17 void access( char *buf) __attribute__ ((noinline));
18 #endif
19 void access( char *buf)
20 {
21   buf[0] = '\0';
22 }
23
24 void bar( int i)
25 {
26     char buf[4 * 1024];
27
28     if ( i > 0)
29     {
30         access( buf);
31         std::cout << i << ". iteration" << std::endl;
32         bar( i - 1);
33     }
34 }
35
36 int main() {
37     int count = 384;
38
39     std::cout << "using segmented_stack stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
40     std::cout << "initial stack size = " << boost::context::segmented_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
41     std::cout << "application should not fail" << std::endl;
42
43     boost::coroutines2::coroutine< void >::push_type sink(
44         [&]( boost::coroutines2::coroutine< void >::pull_type & source) {
45             bar( count);
46             source();
47         });
48     
49     sink();
50
51     std::cout << "main: Done" << std::endl;
52
53     return 0;
54 }