segmented_stack.cpp 1.8 KB

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