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/bind.hpp> |
14 | |
15 | #include <iostream> |
16 | |
17 | static void |
18 | display_mallinfo() |
19 | { |
20 | struct mallinfo mi; |
21 | |
22 | mi = mallinfo(); |
23 | |
24 | printf(format: "Total non-mmapped bytes (arena): %d\n" , mi.arena); |
25 | printf(format: "# of free chunks (ordblks): %d\n" , mi.ordblks); |
26 | printf(format: "# of free fastbin blocks (smblks): %d\n" , mi.smblks); |
27 | printf(format: "# of mapped regions (hblks): %d\n" , mi.hblks); |
28 | printf(format: "Bytes in mapped regions (hblkhd): %d\n" , mi.hblkhd); |
29 | printf(format: "Max. total allocated space (usmblks): %d\n" , mi.usmblks); |
30 | printf(format: "Free bytes held in fastbins (fsmblks): %d\n" , mi.fsmblks); |
31 | printf(format: "Total allocated space (uordblks): %d\n" , mi.uordblks); |
32 | printf(format: "Total free space (fordblks): %d\n" , mi.fordblks); |
33 | printf(format: "Topmost releasable block (keepcost): %d\n" , mi.keepcost); |
34 | } |
35 | |
36 | boost::mutex io_mutex; |
37 | |
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 | } |
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( rel_time: boost::posix_time::milliseconds( 100 ) ); |
56 | |
57 | } |
58 | return 0; |
59 | } |
60 | |
61 | int main() { |
62 | printf(format: "\n============== sizeof(boost::thread) ============== %d\n" , sizeof(boost::thread)); |
63 | printf(format: "\n============== sizeof(boost::detail::thread_data_base) ============== %d\n" , sizeof(boost::detail::thread_data_base)); |
64 | printf(format: "\n============== sizeof(boost::detail::thread_data<>) ============== %d\n" , sizeof(boost::detail::thread_data<void(*)()>)); |
65 | printf(format: "\n============== Before thread creation ==============\n" ); |
66 | display_mallinfo(); |
67 | { |
68 | boost::thread thrd1(&count); |
69 | |
70 | printf(format: "\n============== After thread creation ==============\n" ); |
71 | display_mallinfo(); |
72 | |
73 | boost::thread thrd2(&count); |
74 | printf(format: "\n============== After thread creation ==============\n" ); |
75 | display_mallinfo(); |
76 | boost::thread thrd3(&count); |
77 | printf(format: "\n============== After thread creation ==============\n" ); |
78 | display_mallinfo(); |
79 | |
80 | thrd1.join(); |
81 | printf(format: "\n============== After thread join ==============\n" ); |
82 | display_mallinfo(); |
83 | |
84 | thrd2.join(); |
85 | printf(format: "\n============== After thread join ==============\n" ); |
86 | display_mallinfo(); |
87 | thrd3.join(); |
88 | printf(format: "\n============== After thread join ==============\n" ); |
89 | display_mallinfo(); |
90 | } |
91 | printf(format: "\n============== After thread destruction ==============\n" ); |
92 | display_mallinfo(); |
93 | |
94 | { |
95 | pthread_attr_t attr; |
96 | pthread_attr_init(attr: &attr); |
97 | |
98 | pthread_t thrd1; |
99 | pthread_create(newthread: &thrd1, attr: &attr, start_routine: &count2, arg: 0); |
100 | printf(format: "\n============== After thread creation ==============\n" ); |
101 | display_mallinfo(); |
102 | |
103 | pthread_t thrd2; |
104 | pthread_create(newthread: &thrd2, attr: &attr, start_routine: &count2, arg: 0); |
105 | printf(format: "\n============== After thread creation ==============\n" ); |
106 | display_mallinfo(); |
107 | |
108 | pthread_t thrd3; |
109 | pthread_create(newthread: &thrd3, attr: &attr, start_routine: &count2, arg: 0); |
110 | printf(format: "\n============== After thread creation ==============\n" ); |
111 | display_mallinfo(); |
112 | |
113 | pthread_attr_destroy(attr: &attr); |
114 | printf(format: "\n============== After thread attr destroy ==============\n" ); |
115 | display_mallinfo(); |
116 | |
117 | void* res; |
118 | pthread_join(th: thrd3, thread_return: &res); |
119 | printf(format: "\n============== After thread join ==============\n" ); |
120 | display_mallinfo(); |
121 | |
122 | pthread_join(th: thrd2, thread_return: &res); |
123 | printf(format: "\n============== After thread join ==============\n" ); |
124 | display_mallinfo(); |
125 | pthread_join(th: thrd1, thread_return: &res); |
126 | printf(format: "\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(format: "\n============== After thread destruction ==============\n" ); |
136 | display_mallinfo(); |
137 | |
138 | return 1; |
139 | |
140 | } |
141 | |