1 2 // Copyright Oliver Kowalke 2013. 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 <iostream> 8 #include <thread> 9 10 #include <boost/assert.hpp> 11 #include <boost/fiber/all.hpp> 12 13 int count = 384; 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 access(char * buf)20void access( char *buf) 21 { 22 buf[0] = '\0'; 23 } 24 bar(int i)25void bar( int i) 26 { 27 char buf[4 * 1024]; 28 29 if ( i > 0) 30 { 31 access( buf); 32 std::cout << i << ". iteration" << std::endl; 33 bar( i - 1); 34 } 35 } 36 foo()37void foo() 38 { 39 bar( count); 40 boost::this_fiber::yield(); 41 } 42 thread_fn()43void thread_fn() 44 { 45 { 46 boost::fibers::fiber f( 47 #if defined(BOOST_USE_SEGMENTED_STACKS) 48 std::allocator_arg, 49 boost::fibers::segmented_stack( 50 boost::fibers::segmented_stack::traits_type::default_size() ), 51 #endif 52 foo); 53 f.join(); 54 } 55 } 56 main(int argc,char * argv[])57int main( int argc, char * argv[]) 58 { 59 #if defined(BOOST_USE_SEGMENTED_STACKS) 60 std::cout << "using segmented_stack stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, "; 61 std::cout << "initial stack size = " << boost::fibers::segmented_stack::traits_type::default_size() / 1024 << "kB" << std::endl; 62 std::cout << "application should not fail" << std::endl; 63 #else 64 std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, "; 65 std::cout << "initial stack size = " << boost::fibers::fixedsize_stack::traits_type::default_size() / 1024 << "kB" << std::endl; 66 std::cout << "application might fail" << std::endl; 67 #endif 68 69 std::thread( thread_fn).join(); 70 71 std::cout << "done." << std::endl; 72 73 return 0; 74 } 75