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 5 #define BOOST_THREAD_VERSION 2 6 #define BOOST_TEST_MODULE Boost.Threads: thread attributes test suite 7 #include <boost/thread/detail/config.hpp> 8 9 #include <boost/thread/thread_only.hpp> 10 #include <boost/thread/xtime.hpp> 11 #include <boost/bind/bind.hpp> 12 #include <boost/ref.hpp> 13 #include <boost/utility.hpp> 14 15 #include <iostream> 16 #include <boost/test/unit_test.hpp> 17 18 #define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_sleep_only 19 #include "./util.inl" 20 21 int test_value; 22 #ifdef PTHREAD_STACK_MIN 23 #define MY_PTHREAD_STACK PTHREAD_STACK_MIN 24 #else 25 #define MY_PTHREAD_STACK 4*0x4000 26 #endif simple_thread()27void simple_thread() 28 { 29 test_value = 999; 30 } 31 BOOST_AUTO_TEST_CASE(test_native_handle)32BOOST_AUTO_TEST_CASE(test_native_handle) 33 { 34 35 boost::thread_attributes attrs; 36 37 boost::thread_attributes::native_handle_type* h = attrs.native_handle(); 38 (void)(h); // unused 39 #if defined(BOOST_THREAD_PLATFORM_WIN32) 40 // ... window version 41 #elif defined(BOOST_THREAD_PLATFORM_PTHREAD) 42 43 int k = pthread_attr_setstacksize(h, MY_PTHREAD_STACK); 44 std::cout << k << std::endl; 45 BOOST_CHECK(!pthread_attr_setstacksize(h, MY_PTHREAD_STACK)); 46 std::size_t res; 47 BOOST_CHECK(!pthread_attr_getstacksize(h, &res)); 48 BOOST_CHECK(res >= (MY_PTHREAD_STACK)); 49 #else 50 #error "Boost thread unavailable on this platform" 51 #endif 52 53 } 54 BOOST_AUTO_TEST_CASE(test_stack_size)55BOOST_AUTO_TEST_CASE(test_stack_size) 56 { 57 boost::thread_attributes attrs; 58 59 attrs.set_stack_size(0x4000); 60 BOOST_CHECK(attrs.get_stack_size() >= 0x4000); 61 62 } 63 do_test_creation_with_attrs()64void do_test_creation_with_attrs() 65 { 66 test_value = 0; 67 boost::thread_attributes attrs; 68 attrs.set_stack_size(0x4000); 69 boost::thread thrd(attrs, &simple_thread); 70 thrd.join(); 71 BOOST_CHECK_EQUAL(test_value, 999); 72 } 73 BOOST_AUTO_TEST_CASE(test_creation_with_attrs)74BOOST_AUTO_TEST_CASE(test_creation_with_attrs) 75 { 76 timed_test(&do_test_creation_with_attrs, 1); 77 } 78 79 80