*: adding a threaded worker example
Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
parent
c89b8f22da
commit
d3a10d7190
4 changed files with 44 additions and 1 deletions
|
@ -1,4 +1,6 @@
|
||||||
|
|
||||||
|
#ifndef ACCOUNT_H
|
||||||
|
#define ACCOUNT_H
|
||||||
/*
|
/*
|
||||||
* =====================================================================================
|
* =====================================================================================
|
||||||
* Class: Account
|
* Class: Account
|
||||||
|
@ -39,3 +41,4 @@ class Account
|
||||||
|
|
||||||
}; /* ----- end of class Account ----- */
|
}; /* ----- end of class Account ----- */
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
4
main.cpp
4
main.cpp
|
@ -25,5 +25,9 @@ using namespace std;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
Account account(0.0);
|
Account account(0.0);
|
||||||
|
account.deposit(100.5); // Add some money
|
||||||
|
account.deposit(50.25); // Add more money
|
||||||
|
std::cout << "Current Balance: " << account.getBalance() << std::endl;
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,5 +8,9 @@ a = run_command('date', check: true)
|
||||||
read_exe = executable('read', 'read.c', link_language : 'c',)
|
read_exe = executable('read', 'read.c', link_language : 'c',)
|
||||||
readpp_exe = executable('read++', 'read++.cpp')
|
readpp_exe = executable('read++', 'read++.cpp')
|
||||||
|
|
||||||
|
worker_exe = executable('worker', 'worker.cpp')
|
||||||
|
|
||||||
account_sources = ['main.cpp', 'account.cpp', 'account.h']
|
account_sources = ['main.cpp', 'account.cpp', 'account.h']
|
||||||
account_exe = executable('account', account_sources)
|
account_exe = executable('account', account_sources,
|
||||||
|
cpp_args: ['-std=c++23'],
|
||||||
|
)
|
||||||
|
|
32
worker.cpp
Normal file
32
worker.cpp
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
// https://en.cppreference.com/w/cpp/thread/thread
|
||||||
|
// https://en.cppreference.com/w/cpp/thread/mutex
|
||||||
|
// https://en.cppreference.com/w/cpp/thread/condition_variable
|
||||||
|
|
||||||
|
std::mutex mtx;
|
||||||
|
std::condition_variable cv;
|
||||||
|
bool ready = false;
|
||||||
|
|
||||||
|
void worker() {
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
cv.wait(lock, [] { return ready; }); // Wait until 'ready' is set to true
|
||||||
|
std::cout << "Daemon process woke up!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::thread daemon(worker);
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
|
ready = true;
|
||||||
|
}
|
||||||
|
cv.notify_one();
|
||||||
|
|
||||||
|
daemon.join();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue