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/context/continuation.hpp>
12
13namespace ctx = boost::context;
14
15#ifdef BOOST_MSVC //MS VisualStudio
16__declspec(noinline) void access( char *buf);
17#else // GCC
18void access( char *buf) __attribute__ ((noinline));
19#endif
20void access( char *buf) {
21 buf[0] = '\0';
22}
23
24void 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: i - 1);
30 }
31}
32
33int main() {
34 int count = 100*1024;
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 = " << ctx::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 = " << ctx::fixedsize_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
42 std::cout << "application might fail" << std::endl;
43#endif
44 ctx::continuation c = ctx::callcc(
45 fn: [count](ctx::continuation && c){
46 bar( i: count);
47 return std::move( c);
48 });
49 std::cout << "main: done" << std::endl;
50 return EXIT_SUCCESS;
51}
52

source code of boost/libs/context/example/callcc/segmented.cpp