From 6e872375fc5ab248d9f24c6c0ddc51518652666f Mon Sep 17 00:00:00 2001 From: Tanner Babcock Date: Sun, 19 Nov 2023 23:51:17 -0600 Subject: [PATCH] Updated some programs, added async.cpp --- .github/workflows/c-cpp.yml | 3 +++ .gitlab-ci.yml | 5 +++++ io/copy.cpp | 2 +- io/read.cpp | 13 +++++------ thread/async.cpp | 36 +++++++++++++++++++++++++++++++ thread/mutex.cpp | 43 +++++++++++++++++++++++-------------- 6 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 thread/async.cpp diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index 2937d0d..82e91b8 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -56,11 +56,13 @@ jobs: cc -Wall -O2 point/linklist.c -o point/linklist cc -Wall -O2 point/linkpop.c -o point/linkpop cc -Wall -O2 point/linkpush.c -o point/linkpush + g++ -Wall -O2 point/list.cpp -o point/list cc -Wall -O2 point/malloc.c -o point/malloc cc -Wall -O2 point/realloc.c -o point/realloc cc -Wall -O2 point/structfunc.c -o point/structfunc cc -Wall -O2 point/union.c -o point/union cc -Wall -O2 point/hashmap.c -o point/hashmap + g++ -Wall -O2 point/hashmap.cpp -o point/hashmap2 cc -Wall -O2 point/hashtable.c -o point/hashtable cc -Wall -O2 sys/daemon.c -o sys/daemon cc -Wall -O2 sys/fork.c -o sys/fork @@ -71,6 +73,7 @@ jobs: cc -Wall -O2 sys/speak.c -o sys/speak cc -Wall -O2 sys/symlink.c -o sys/symlink cc -Wall -O2 sys/tick.c -o sys/tick + g++ -Wall -O2 thread/async.cpp -o thread/async cc -Wall -O2 thread/condsignal.c -lpthread -o thread/condsignal cc -Wall -O2 thread/mutex.c -lpthread -o thread/mutex g++ -Wall -O2 -pthread -std=c++11 thread/mutex.cpp -o thread/mutex2 diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 32c0a8e..ec07aba 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -44,11 +44,13 @@ build: - cc -Wall -O2 point/linklist.c -o point/linklist - cc -Wall -O2 point/linkpop.c -o point/linkpop - cc -Wall -O2 point/linkpush.c -o point/linkpush + - g++ -Wall -O2 point/list.cpp -o point/list - cc -Wall -O2 point/malloc.c -o point/malloc - cc -Wall -O2 point/realloc.c -o point/realloc - cc -Wall -O2 point/structfunc.c -o point/structfunc - cc -Wall -O2 point/union.c -o point/union - cc -Wall -O2 point/hashmap.c -o point/hashmap + - g++ -Wall -O2 point/hashmap.cpp -o point/hashmap2 - cc -Wall -O2 point/hashtable.c -o point/hashtable - cc -Wall -O2 sys/daemon.c -o sys/daemon - cc -Wall -O2 sys/fork.c -o sys/fork @@ -61,6 +63,7 @@ build: - cc -Wall -O2 sys/tick.c -o sys/tick - cc -Wall -O2 thread/condsignal.c -lpthread -o thread/condsignal - cc -Wall -O2 thread/mutex.c -lpthread -o thread/mutex + - g++ -Wall -O2 -pthread thread/async.cpp -o thread/async - g++ -Wall -O2 -pthread -std=c++11 thread/mutex.cpp -o thread/mutex2 - g++ -Wall -O2 -pthread thread/thread.cpp -o thread/thread - cc -Wall -O2 thread/threads.c -lpthread -o thread/threads @@ -123,6 +126,7 @@ build: - point/structfunc - point/union - point/hashmap + - point/hashmap2 - point/hashtable - sys/daemon - sys/fork @@ -133,6 +137,7 @@ build: - sys/speak - sys/symlink - sys/tick + - thread/async - thread/condsignal - thread/mutex - thread/mutex2 diff --git a/io/copy.cpp b/io/copy.cpp index d3bb73e..d7d2084 100644 --- a/io/copy.cpp +++ b/io/copy.cpp @@ -15,7 +15,7 @@ int main(int argc, char *argv[]) { in->open(argv[1]); if (!in) { - cerr << "ERROR: Input file does not exist" << endl; + cerr << "ERROR: Input file '" << argv[1] << "' does not exist" << endl; return 2; } diff --git a/io/read.cpp b/io/read.cpp index c583f51..3859010 100644 --- a/io/read.cpp +++ b/io/read.cpp @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) { in[x-1].open(argv[x]); if (!in[x-1]) { in[x-1].close(); - cerr << "ERROR: File not found" << endl; + cerr << "ERROR: '" << argv[x] << "' File not found" << endl; delete[] in; return 2; } @@ -29,17 +29,18 @@ int main(int argc, char *argv[]) { delete[] in; } else if (argc == 1) { - ifstream in; - in.open(argv[1]); + ifstream *in = new ifstream(); + in->open(argv[1]); if (!in) { - cerr << "ERROR: File not found" << endl; + cerr << "ERROR: '" << argv[1] << "' File not found" << endl; return 2; } char x; - while (in.get(x)) // fails on EOF + while (in->get(x)) // fails on EOF cout.put(x); - in.close(); + in->close(); + delete in; } return 0; } diff --git a/thread/async.cpp b/thread/async.cpp new file mode 100644 index 0000000..b8c11d9 --- /dev/null +++ b/thread/async.cpp @@ -0,0 +1,36 @@ +/* Async fizzbuzz + * Tanner Babcock + * November 19, 2023 */ +#define _GLIBCXX_USE_NANOSLEEP +#include +#include +#include + +void callback(const std::string& data) { + std::cout << "Callback called with: " << data << std::endl; +} + +void task(int time) { + std::this_thread::sleep_for(std::chrono::seconds(time)); + callback("async task done"); +} + +int main(void) { + std::thread *bt = new std::thread(task, 1); + std::cout << "async task started" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(3)); + for (int x = 0; x < 100; x++) { + if (x % 5 == 0) + std::cout << "buzz"; + if (x % 3 == 0) + std::cout << "fizz"; + else if (x % 5 != 0) + std::cout << x; + std::cout << std::endl; + } + std::cout << "main done" << std::endl; + bt->join(); + delete bt; + return 0; +} + diff --git a/thread/mutex.cpp b/thread/mutex.cpp index 0428bb2..0f25f43 100644 --- a/thread/mutex.cpp +++ b/thread/mutex.cpp @@ -1,5 +1,5 @@ /* Job queue with mutex - * July 10, 2022 + * July 10, 2022, updated November 2023 * Compile with: * g++ -pthread -std=c++11 mutex.cpp */ #include @@ -14,9 +14,9 @@ struct job { int num; }; bool closed = false; -std::deque jobList; -std::mutex jobMutex; -std::condition_variable jobCondition; +std::deque *jobList; +std::mutex *jobMutex; +std::condition_variable *jobCondition; std::atomic threadsRunning; /* thread-safe cout */ @@ -37,10 +37,10 @@ std::mutex tcout::mutex; void addJobs(void) { static int num = 0; job current = { num++ }; - std::unique_lock lock(jobMutex); - jobList.push_back(current); + std::unique_lock lock(*jobMutex); + jobList->push_back(current); - jobCondition.notify_one(); + jobCondition->notify_one(); lock.unlock(); } @@ -50,16 +50,17 @@ void work(int seconds) { job current; threadsRunning++; while (true) { - std::unique_lock lock(jobMutex); - if (jobList.empty()) { + std::unique_lock lock(*jobMutex); + + if (jobList->empty()) { threadsRunning--; - jobCondition.wait(lock, [] { return !jobList.empty() || closed; }); + jobCondition->wait(lock, [] { return !jobList->empty() || closed; }); threadsRunning++; } - if (jobList.empty()) + if (jobList->empty()) break; - current = jobList.front(); - jobList.pop_front(); + current = jobList->front(); + jobList->pop_front(); lock.unlock(); if ((current.num % 2) == 0) @@ -79,6 +80,7 @@ void work(int seconds) { int main(int argc, char *argv[]) { int res; + if (argc < 2) { std::cout << "Number of threads to use (20 max): "; std::cin >> res; @@ -99,6 +101,10 @@ int main(int argc, char *argv[]) { return 2; } + jobList = new std::deque(); + jobMutex = new std::mutex(); + jobCondition = new std::condition_variable(); + std::thread jobThreads[res]; for (int i = 0; i < res; i++) { @@ -108,13 +114,18 @@ int main(int argc, char *argv[]) { addJobs(); } { - std::unique_lock lock(jobMutex); + std::unique_lock lock(*jobMutex); closed = true; - jobCondition.notify_all(); + jobCondition->notify_all(); } for (int i = 0; i < res; i++) { jobThreads[i].join(); - } + } + std::cout << "Finished" << std::endl; + + delete jobCondition; + delete jobMutex; + delete jobList; return 0; }