From 31293dd9b427be45175a928cf55f90ea1bccebeb Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 3 Jun 2023 16:00:17 +0200 Subject: multithreaded processing --- os2eindopdracht/.gitignore | 2 ++ os2eindopdracht/SampleStream.cpp | 3 +++ os2eindopdracht/SampleStream.h | 1 + os2eindopdracht/main.cpp | 16 +++++++++++++++- os2eindopdracht/makefile | 2 +- 5 files changed, 22 insertions(+), 2 deletions(-) (limited to 'os2eindopdracht') diff --git a/os2eindopdracht/.gitignore b/os2eindopdracht/.gitignore index 7c39037..181de15 100644 --- a/os2eindopdracht/.gitignore +++ b/os2eindopdracht/.gitignore @@ -5,3 +5,5 @@ main *.wav *.mp3 *.flac +.cache +compile_commands.json diff --git a/os2eindopdracht/SampleStream.cpp b/os2eindopdracht/SampleStream.cpp index 3727e52..e97c474 100644 --- a/os2eindopdracht/SampleStream.cpp +++ b/os2eindopdracht/SampleStream.cpp @@ -40,3 +40,6 @@ vector SampleStream::split(size_t block_size) { return out; } +void SampleStream::align(size_t block_size) { + return; +} diff --git a/os2eindopdracht/SampleStream.h b/os2eindopdracht/SampleStream.h index 4c8b5c3..5657b06 100644 --- a/os2eindopdracht/SampleStream.h +++ b/os2eindopdracht/SampleStream.h @@ -16,6 +16,7 @@ public: void load(string input); string save(); vector split(size_t block_size); + void align(size_t block_size); public: vector _stream; 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 #include #include +#include +#include #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 output_blocks = output.split(SAMPLE_BLOCK_SIZE); + counting_semaphore semaphore(args.max_threads); + vector 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()); diff --git a/os2eindopdracht/makefile b/os2eindopdracht/makefile index 3d8f260..eb86336 100644 --- a/os2eindopdracht/makefile +++ b/os2eindopdracht/makefile @@ -1,7 +1,7 @@ C++ = g++ LD = g++ RM = rm -f -CFLAGS = +CFLAGS = -std=c++20 LFLAGS = TARGET = main -- cgit v1.2.3