Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / coroutine / example / symmetric / segmented_stack.cpp
1
2 //          Copyright Oliver Kowalke 2009.
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 <boost/coroutine/all.hpp>
8
9 #include <iostream>
10
11 #include <boost/assert.hpp>
12 #include <boost/config.hpp>
13 #include <boost/thread.hpp>
14
15 int count = 384;
16
17 #ifdef BOOST_MSVC //MS VisualStudio
18 __declspec(noinline) void access( char *buf);
19 #else // GCC
20 void access( char *buf) __attribute__ ((noinline));
21 #endif
22 void access( char *buf)
23 {
24   buf[0] = '\0';
25 }
26
27 void bar( int i)
28 {
29     char buf[4 * 1024];
30
31     if ( i > 0)
32     {
33         access( buf);
34         std::cout << i << ". iteration" << std::endl;
35         bar( i - 1);
36     }
37 }
38
39 void foo( boost::coroutines::symmetric_coroutine< void >::yield_type &)
40 {
41     bar( count);
42 }
43
44 void thread_fn()
45 {
46     {
47         boost::coroutines::symmetric_coroutine< void >::call_type coro( foo);
48         coro();
49     }
50 }
51
52 int main( int argc, char * argv[])
53 {
54 #if defined(BOOST_USE_SEGMENTED_STACKS)
55     std::cout << "using segmented stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
56     std::cout << "initial stack size = " << boost::coroutines::stack_allocator::traits_type::default_size() / 1024 << "kB" << std::endl;
57     std::cout << "application should not fail" << std::endl;
58 #else
59     std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
60     std::cout << "initial stack size = " << boost::coroutines::stack_allocator::traits_type::default_size() / 1024 << "kB" << std::endl;
61     std::cout << "application might fail" << std::endl;
62 #endif
63
64     boost::thread( thread_fn).join();
65
66     return 0;
67 }