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