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