File indexing completed on 2025-01-19 08:22:20
0001 /* 0002 SPDX-FileCopyrightText: 2014-2017 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include <future> 0008 #include <thread> 0009 #include <vector> 0010 0011 using namespace std; 0012 0013 const int ALLOCS_PER_THREAD = 1000; 0014 0015 int** alloc() 0016 { 0017 int** block = new int*[ALLOCS_PER_THREAD]; 0018 for (int i = 0; i < ALLOCS_PER_THREAD; ++i) { 0019 block[i] = new int; 0020 } 0021 return block; 0022 } 0023 0024 void dealloc(future<int**>&& f) 0025 { 0026 int** block = f.get(); 0027 for (int i = 0; i < ALLOCS_PER_THREAD; ++i) { 0028 delete block[i]; 0029 } 0030 delete[] block; 0031 } 0032 0033 int main() 0034 { 0035 vector<future<void>> futures; 0036 futures.reserve(100 * 4); 0037 for (int i = 0; i < 100; ++i) { 0038 auto f1 = async(launch::async, alloc); 0039 auto f2 = async(launch::async, alloc); 0040 auto f3 = async(launch::async, alloc); 0041 auto f4 = async(launch::async, alloc); 0042 futures.emplace_back(async(launch::async, dealloc, std::move(f1))); 0043 futures.emplace_back(async(launch::async, dealloc, std::move(f2))); 0044 futures.emplace_back(async(launch::async, dealloc, std::move(f3))); 0045 futures.emplace_back(async(launch::async, dealloc, std::move(f4))); 0046 } 0047 return 0; 0048 }