1 | #include <cstdlib> |
---|---|
2 | #include <cstring> |
3 | #include <vector> |
4 | #include <thread> |
5 | |
6 | |
7 | void thread_func() |
8 | { |
9 | for (size_t i = 0; i < 100; ++i) |
10 | { |
11 | size_t size = 4096; |
12 | |
13 | void * buf = malloc(size); |
14 | if (!buf) |
15 | abort(); |
16 | memset(buf, 0, size); |
17 | |
18 | while (size < 1048576) |
19 | { |
20 | size_t next_size = size * 4; |
21 | |
22 | void * new_buf = realloc(buf, next_size); |
23 | if (!new_buf) |
24 | abort(); |
25 | buf = new_buf; |
26 | |
27 | memset(reinterpret_cast<char*>(buf) + size, 0, next_size - size); |
28 | size = next_size; |
29 | } |
30 | |
31 | free(buf); |
32 | } |
33 | } |
34 | |
35 | |
36 | int main(int, char **) |
37 | { |
38 | std::vector<std::thread> threads(16); |
39 | for (size_t i = 0; i < 1000; ++i) |
40 | { |
41 | for (auto & thread : threads) |
42 | thread = std::thread(thread_func); |
43 | for (auto & thread : threads) |
44 | thread.join(); |
45 | } |
46 | return 0; |
47 | } |
48 |