-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathThreaded.cpp
More file actions
31 lines (25 loc) · 788 Bytes
/
Threaded.cpp
File metadata and controls
31 lines (25 loc) · 788 Bytes
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
#include <cstddef>
#include <future>
#include <mutex>
#include <string>
#include <string_view>
#include <vector>
#include "StringPool.h"
int main()
{
const std::string string(1000, 'a');
const std::string_view stringView{ string };
StringPool<char> pool;
std::mutex poolMutex;
std::vector<std::string_view> strings;
std::vector<std::future<void>> futures;
for (std::size_t i = 0; i < 1000; ++i) {
futures.push_back(std::async(std::launch::async, [&pool, &poolMutex, stringView, &strings](){
std::scoped_lock lockForAddingStrings{ poolMutex };
for (std::size_t j = 0; j < 1000; ++j)
strings.push_back(pool.add(stringView));
}));
}
for (const auto& future : futures)
future.wait();
}