Fixed issue with threads leftovers
This commit is contained in:
parent
a4f722f6fb
commit
d7ea7c8866
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,3 +6,4 @@ gmon\.out
|
||||
cmake-build-debug/
|
||||
|
||||
\.idea/
|
||||
*.lzw
|
||||
|
@ -116,18 +116,18 @@ void compress(const std::string &t_in_file, const char *t_out_file) {
|
||||
// const auto comp_str{lzw_compress(str, dictionary)};
|
||||
|
||||
// thread pool
|
||||
std::vector<std::pair<std::thread, uvec>> threads{};
|
||||
std::vector<std::pair<std::unique_ptr<std::thread>, uvec>> threads{};
|
||||
|
||||
// char chunk[32768];
|
||||
std::vector<char> chunk{};
|
||||
chunk.reserve(32768);
|
||||
while (input_file.read(chunk.data(), 32768)) {
|
||||
threads.push_back(std::make_pair(std::thread{}, uvec{}));
|
||||
threads.back().first =
|
||||
std::thread{lzw_compress, chunk, ref(threads.back().second)};
|
||||
threads.push_back(std::make_pair(nullptr, uvec{}));
|
||||
threads.back().first = std::make_unique<std::thread>(
|
||||
std::thread{lzw_compress, chunk, ref(threads.back().second)});
|
||||
if (threads.size() >= 8) {
|
||||
for (auto &elem : threads) {
|
||||
elem.first.join();
|
||||
(*elem.first).join();
|
||||
}
|
||||
for (auto &elem : threads) {
|
||||
for (const auto c : elem.second) {
|
||||
@ -138,6 +138,18 @@ void compress(const std::string &t_in_file, const char *t_out_file) {
|
||||
}
|
||||
}
|
||||
|
||||
if(threads.size() != 0) {
|
||||
for (auto &elem : threads) {
|
||||
(*elem.first).join();
|
||||
}
|
||||
for (auto &elem : threads) {
|
||||
for (const auto c : elem.second) {
|
||||
write_utf8(out, c);
|
||||
}
|
||||
}
|
||||
threads.clear();
|
||||
}
|
||||
|
||||
if(input_file.tellg() != std::ios::end) {
|
||||
std::puts("Leftovers...");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user