segmented_stack.cpp 1.7 KB

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