From d7ea7c8866ce443c727c2e06f896d5d20d72939a Mon Sep 17 00:00:00 2001 From: Phuntsok Drak-pa Date: Thu, 26 Apr 2018 11:59:09 +0200 Subject: [PATCH] Fixed issue with threads leftovers --- .gitignore | 1 + src/compress.cc | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e4c99a6..c5a5fba 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ gmon\.out cmake-build-debug/ \.idea/ +*.lzw diff --git a/src/compress.cc b/src/compress.cc index d3a040a..0278200 100644 --- a/src/compress.cc +++ b/src/compress.cc @@ -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> threads{}; + std::vector, uvec>> threads{}; // char chunk[32768]; std::vector 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{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..."); }