test_8557.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2013 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // B
  6. #include <malloc.h>
  7. #include <boost/thread/thread.hpp>
  8. #include <boost/thread/mutex.hpp>
  9. #include <boost/bind.hpp>
  10. #include <iostream>
  11. static void
  12. display_mallinfo()
  13. {
  14. struct mallinfo mi;
  15. mi = mallinfo();
  16. printf("Total non-mmapped bytes (arena): %d\n", mi.arena);
  17. printf("# of free chunks (ordblks): %d\n", mi.ordblks);
  18. printf("# of free fastbin blocks (smblks): %d\n", mi.smblks);
  19. printf("# of mapped regions (hblks): %d\n", mi.hblks);
  20. printf("Bytes in mapped regions (hblkhd): %d\n", mi.hblkhd);
  21. printf("Max. total allocated space (usmblks): %d\n", mi.usmblks);
  22. printf("Free bytes held in fastbins (fsmblks): %d\n", mi.fsmblks);
  23. printf("Total allocated space (uordblks): %d\n", mi.uordblks);
  24. printf("Total free space (fordblks): %d\n", mi.fordblks);
  25. printf("Topmost releasable block (keepcost): %d\n", mi.keepcost);
  26. }
  27. boost::mutex io_mutex;
  28. void count() {
  29. for (int i = 0; i < 10; ++i) {
  30. boost::mutex::scoped_lock lock(io_mutex);
  31. //boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
  32. }
  33. }
  34. void* count2(void*) {
  35. for (int i = 0; i < 10; ++i) {
  36. boost::mutex::scoped_lock lock(io_mutex);
  37. boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
  38. }
  39. return 0;
  40. }
  41. int main() {
  42. printf("\n============== sizeof(boost::thread) ============== %d\n", sizeof(boost::thread));
  43. printf("\n============== sizeof(boost::detail::thread_data_base) ============== %d\n", sizeof(boost::detail::thread_data_base));
  44. printf("\n============== sizeof(boost::detail::thread_data<>) ============== %d\n", sizeof(boost::detail::thread_data<void(*)()>));
  45. printf("\n============== Before thread creation ==============\n");
  46. display_mallinfo();
  47. {
  48. boost::thread thrd1(&count);
  49. printf("\n============== After thread creation ==============\n");
  50. display_mallinfo();
  51. boost::thread thrd2(&count);
  52. printf("\n============== After thread creation ==============\n");
  53. display_mallinfo();
  54. boost::thread thrd3(&count);
  55. printf("\n============== After thread creation ==============\n");
  56. display_mallinfo();
  57. thrd1.join();
  58. printf("\n============== After thread join ==============\n");
  59. display_mallinfo();
  60. thrd2.join();
  61. printf("\n============== After thread join ==============\n");
  62. display_mallinfo();
  63. thrd3.join();
  64. printf("\n============== After thread join ==============\n");
  65. display_mallinfo();
  66. }
  67. printf("\n============== After thread destruction ==============\n");
  68. display_mallinfo();
  69. {
  70. pthread_attr_t attr;
  71. pthread_attr_init(&attr);
  72. pthread_t thrd1;
  73. pthread_create(&thrd1, &attr, &count2, 0);
  74. printf("\n============== After thread creation ==============\n");
  75. display_mallinfo();
  76. pthread_t thrd2;
  77. pthread_create(&thrd2, &attr, &count2, 0);
  78. printf("\n============== After thread creation ==============\n");
  79. display_mallinfo();
  80. pthread_t thrd3;
  81. pthread_create(&thrd3, &attr, &count2, 0);
  82. printf("\n============== After thread creation ==============\n");
  83. display_mallinfo();
  84. pthread_attr_destroy(&attr);
  85. printf("\n============== After thread attr destroy ==============\n");
  86. display_mallinfo();
  87. void* res;
  88. pthread_join(thrd3, &res);
  89. printf("\n============== After thread join ==============\n");
  90. display_mallinfo();
  91. pthread_join(thrd2, &res);
  92. printf("\n============== After thread join ==============\n");
  93. display_mallinfo();
  94. pthread_join(thrd1, &res);
  95. printf("\n============== After thread join ==============\n");
  96. display_mallinfo();
  97. //pthread_destroy(&thrd1);
  98. //pthread_destroy(&thrd2);
  99. //pthread_destroy(&thrd3);
  100. }
  101. printf("\n============== After thread destruction ==============\n");
  102. display_mallinfo();
  103. return 1;
  104. }