Imported Upstream version 1.63.0
[platform/upstream/boost.git] / libs / context / example / v1 / 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 #include <memory>
10
11 #include <boost/config.hpp>
12
13 #include <boost/context/all.hpp>
14
15 #ifdef BOOST_MSVC //MS VisualStudio
16 __declspec(noinline) void access( char *buf);
17 #else // GCC
18 void access( char *buf) __attribute__ ((noinline));
19 #endif
20 void access( char *buf) {
21   buf[0] = '\0';
22 }
23
24 void bar( int i) {
25     char buf[4 * 1024];
26     if ( i > 0) {
27         access( buf);
28         std::cout << i << ". iteration" << std::endl;
29         bar( i - 1);
30     }
31 }
32
33 int main() {
34     int count = 384;
35 #if defined(BOOST_USE_SEGMENTED_STACKS)
36     std::cout << "using segmented_stack stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
37     std::cout << "initial stack size = " << boost::context::segmented_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
38     std::cout << "application should not fail" << std::endl;
39 #else
40     std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
41     std::cout << "initial stack size = " << boost::context::fixedsize_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
42     std::cout << "application might fail" << std::endl;
43 #endif
44     boost::context::execution_context main_ctx(
45         boost::context::execution_context::current() );
46     boost::context::execution_context bar_ctx(
47         [& main_ctx, count]( void *){
48             bar( count);
49             main_ctx();   
50         });
51     bar_ctx();
52     std::cout << "main: done" << std::endl;
53     return EXIT_SUCCESS;
54 }