segmented.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright Oliver Kowalke 2014.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <memory>
  8. #include <boost/coroutine2/all.hpp>
  9. #ifdef BOOST_MSVC //MS VisualStudio
  10. __declspec(noinline) void access( char *buf);
  11. #else // GCC
  12. void access( char *buf) __attribute__ ((noinline));
  13. #endif
  14. void access( char *buf) {
  15. buf[0] = '\0';
  16. }
  17. void bar( int i) {
  18. char buf[4 * 1024];
  19. if ( i > 0) {
  20. access( buf);
  21. std::cout << i << ". iteration" << std::endl;
  22. bar( i - 1);
  23. }
  24. }
  25. int main() {
  26. int count = 384;
  27. #if defined(BOOST_USE_SEGMENTED_STACKS)
  28. std::cout << "using segmented_stack stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
  29. std::cout << "initial stack size = " << boost::context::segmented_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
  30. std::cout << "application should not fail" << std::endl;
  31. #else
  32. std::cout << "using standard stacks: allocates " << count << " * 4kB == " << 4 * count << "kB on stack, ";
  33. std::cout << "initial stack size = " << boost::context::fixedsize_stack::traits_type::default_size() / 1024 << "kB" << std::endl;
  34. std::cout << "application might fail" << std::endl;
  35. #endif
  36. boost::coroutines2::coroutine< void >::pull_type coro{
  37. [count](boost::coroutines2::coroutine< void >::push_type & coro){
  38. bar( count);
  39. }};
  40. std::cout << "main: done" << std::endl;
  41. return EXIT_SUCCESS;
  42. }