test_2741.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (C) 2008 Vicente J. Botet Escriba
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. #define BOOST_THREAD_VERSION 2
  5. #define BOOST_TEST_MODULE Boost.Threads: thread attributes test suite
  6. #include <boost/thread/detail/config.hpp>
  7. #include <boost/thread/thread_only.hpp>
  8. #include <boost/thread/xtime.hpp>
  9. #include <boost/bind.hpp>
  10. #include <boost/ref.hpp>
  11. #include <boost/utility.hpp>
  12. #include <iostream>
  13. #include <boost/test/unit_test.hpp>
  14. #define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_sleep_only
  15. #include "./util.inl"
  16. int test_value;
  17. #ifdef PTHREAD_STACK_MIN
  18. #define MY_PTHREAD_STACK PTHREAD_STACK_MIN
  19. #else
  20. #define MY_PTHREAD_STACK 4*0x4000
  21. #endif
  22. void simple_thread()
  23. {
  24. test_value = 999;
  25. }
  26. BOOST_AUTO_TEST_CASE(test_native_handle)
  27. {
  28. boost::thread_attributes attrs;
  29. boost::thread_attributes::native_handle_type* h = attrs.native_handle();
  30. (void)(h); // unused
  31. #if defined(BOOST_THREAD_PLATFORM_WIN32)
  32. // ... window version
  33. #elif defined(BOOST_THREAD_PLATFORM_PTHREAD)
  34. int k = pthread_attr_setstacksize(h, MY_PTHREAD_STACK);
  35. std::cout << k << std::endl;
  36. BOOST_CHECK(!pthread_attr_setstacksize(h, MY_PTHREAD_STACK));
  37. std::size_t res;
  38. BOOST_CHECK(!pthread_attr_getstacksize(h, &res));
  39. BOOST_CHECK(res >= (MY_PTHREAD_STACK));
  40. #else
  41. #error "Boost thread unavailable on this platform"
  42. #endif
  43. }
  44. BOOST_AUTO_TEST_CASE(test_stack_size)
  45. {
  46. boost::thread_attributes attrs;
  47. attrs.set_stack_size(0x4000);
  48. BOOST_CHECK(attrs.get_stack_size() >= 0x4000);
  49. }
  50. void do_test_creation_with_attrs()
  51. {
  52. test_value = 0;
  53. boost::thread_attributes attrs;
  54. attrs.set_stack_size(0x4000);
  55. boost::thread thrd(attrs, &simple_thread);
  56. thrd.join();
  57. BOOST_CHECK_EQUAL(test_value, 999);
  58. }
  59. BOOST_AUTO_TEST_CASE(test_creation_with_attrs)
  60. {
  61. timed_test(&do_test_creation_with_attrs, 1);
  62. }