Skip to content

Commit

Permalink
Updated some programs, added async.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Babkock committed Nov 20, 2023
1 parent e35ea6a commit 6e87237
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -123,6 +126,7 @@ build:
- point/structfunc
- point/union
- point/hashmap
- point/hashmap2
- point/hashtable
- sys/daemon
- sys/fork
Expand All @@ -133,6 +137,7 @@ build:
- sys/speak
- sys/symlink
- sys/tick
- thread/async
- thread/condsignal
- thread/mutex
- thread/mutex2
Expand Down
2 changes: 1 addition & 1 deletion io/copy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 7 additions & 6 deletions io/read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
36 changes: 36 additions & 0 deletions thread/async.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Async fizzbuzz
* Tanner Babcock
* November 19, 2023 */
#define _GLIBCXX_USE_NANOSLEEP
#include <iostream>
#include <string>
#include <thread>

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;
}

43 changes: 27 additions & 16 deletions thread/mutex.cpp
Original file line number Diff line number Diff line change
@@ -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 <queue>
Expand All @@ -14,9 +14,9 @@
struct job { int num; };
bool closed = false;

std::deque<job> jobList;
std::mutex jobMutex;
std::condition_variable jobCondition;
std::deque<job> *jobList;
std::mutex *jobMutex;
std::condition_variable *jobCondition;
std::atomic<int> threadsRunning;

/* thread-safe cout */
Expand All @@ -37,10 +37,10 @@ std::mutex tcout::mutex;
void addJobs(void) {
static int num = 0;
job current = { num++ };
std::unique_lock<std::mutex> lock(jobMutex);
jobList.push_back(current);
std::unique_lock<std::mutex> lock(*jobMutex);
jobList->push_back(current);

jobCondition.notify_one();
jobCondition->notify_one();
lock.unlock();
}

Expand All @@ -50,16 +50,17 @@ void work(int seconds) {
job current;
threadsRunning++;
while (true) {
std::unique_lock<std::mutex> lock(jobMutex);
if (jobList.empty()) {
std::unique_lock<std::mutex> 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)
Expand All @@ -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;
Expand All @@ -99,6 +101,10 @@ int main(int argc, char *argv[]) {
return 2;
}

jobList = new std::deque<job>();
jobMutex = new std::mutex();
jobCondition = new std::condition_variable();

std::thread jobThreads[res];

for (int i = 0; i < res; i++) {
Expand All @@ -108,13 +114,18 @@ int main(int argc, char *argv[]) {
addJobs();
}
{
std::unique_lock<std::mutex> lock(jobMutex);
std::unique_lock<std::mutex> 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;
}

0 comments on commit 6e87237

Please sign in to comment.