diff options
Diffstat (limited to 'os2eindopdracht/main.cpp')
-rw-r--r-- | os2eindopdracht/main.cpp | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/os2eindopdracht/main.cpp b/os2eindopdracht/main.cpp index 6d8ddd5..bc7ce17 100644 --- a/os2eindopdracht/main.cpp +++ b/os2eindopdracht/main.cpp @@ -3,6 +3,8 @@ #include <sstream> #include <fstream> #include <vector> +#include <semaphore> +#include <thread> #include "Arguments.h" #include "SampleStream.h" @@ -10,6 +12,8 @@ #include "FilterWorker.h" using std::vector; +using std::counting_semaphore; +using std::thread; #define SAMPLE_BLOCK_SIZE 1024 @@ -27,10 +31,20 @@ int main(int argc, char** argv) { SampleStream output(file_input_content.str()); vector<SampleBlock> output_blocks = output.split(SAMPLE_BLOCK_SIZE); + counting_semaphore semaphore(args.max_threads); + vector<thread> threads; for (size_t i = 0; i < input_blocks.size(); i++) { - FilterWorker(input_blocks[i], output_blocks[i]).filter(args.bass, args.treble); + semaphore.acquire(); + threads.push_back(thread([&args, &semaphore](SampleBlock& input, SampleBlock& output) { + FilterWorker(input, output).filter(args.bass, args.treble); + semaphore.release(); + }, std::ref(input_blocks[i]), std::ref(output_blocks[i]))); } + for (thread& t : threads) t.join(); + + output.align(SAMPLE_BLOCK_SIZE); + std::fstream file_output(args.file_output, std::ios::out | std::ios::binary); std::string edited_stream = output.save(); file_output.write(edited_stream.c_str(), edited_stream.size()); |