Skip to content

Commit

Permalink
Add Timer code
Browse files Browse the repository at this point in the history
  • Loading branch information
rdong8 committed Jan 4, 2024
1 parent 32401d8 commit 508ea6f
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 252 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PROJECT = cpp_project
PROJECT = learn_asio

CC = clang
CXX = clang++
Expand All @@ -10,7 +10,7 @@ BUILD_TYPE = Debug
system-deps:
sudo dnf install llvm compiler-rt cmake doxygen

.PHONY:
.PHONY: py-deps
py-deps: requirements.txt
pyenv virtualenv 3.12.1 ${PROJECT}
pyenv local ${PROJECT}
Expand Down
33 changes: 18 additions & 15 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
find_package(Boost REQUIRED)

add_library(mathematics)
target_sources(mathematics PUBLIC FILE_SET CXX_MODULES FILES mathematics.ccm)
target_link_libraries(mathematics PRIVATE my_config)
add_executable(Timer.1)
target_sources(Timer.1 PRIVATE Timer/1.cc)
target_link_libraries(Timer.1 PRIVATE my_config Boost::system)

add_executable(cpp_project)
target_sources(cpp_project PRIVATE main.cc)
target_link_libraries(
cpp_project
PRIVATE my_config
Boost::system
Boost::coroutine
Boost::regex
Boost::thread
Boost::date_time
Boost::serialization
mathematics)
add_executable(Timer.2)
target_sources(Timer.2 PRIVATE Timer/2.cc)
target_link_libraries(Timer.2 PRIVATE my_config Boost::system)

add_executable(Timer.3)
target_sources(Timer.3 PRIVATE Timer/3.cc)
target_link_libraries(Timer.3 PRIVATE my_config Boost::system)

add_executable(Timer.4)
target_sources(Timer.4 PRIVATE Timer/4.cc)
target_link_libraries(Timer.4 PRIVATE my_config Boost::system)

add_executable(Timer.5)
target_sources(Timer.5 PRIVATE Timer/5.cc)
target_link_libraries(Timer.5 PRIVATE my_config Boost::system)
14 changes: 14 additions & 0 deletions src/Timer/1.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>

#include <boost/asio.hpp>

auto main() -> int {
boost::asio::io_context io{};
boost::asio::steady_timer t{io, boost::asio::chrono::seconds{5}};

t.wait();

std::cout << "Hello, world!" << std::endl;

return 0;
}
16 changes: 16 additions & 0 deletions src/Timer/2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>

#include <boost/asio.hpp>

auto main() -> int {
boost::asio::io_context io{};
boost::asio::steady_timer t{io, boost::asio::chrono::seconds{5}};

t.async_wait([](const boost::system::error_code&) {
std::cout << "Hello, world!" << std::endl;
});

io.run();

return 0;
}
44 changes: 44 additions & 0 deletions src/Timer/3.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <functional>
#include <iostream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>

namespace {
auto print(const boost::system::error_code&, boost::asio::steady_timer& t, int& count) -> void {
if (count >= 5) {
return;
}

std::cout << count << std::endl;
++count;

// Delay expiry by 1 second
t.expires_at(t.expiry() + boost::asio::chrono::seconds{1});

// Start new async wait on the timer
t.async_wait(boost::bind(
print,
boost::asio::placeholders::error,
std::ref(t),
std::ref(count)));
}
}

auto main() -> int {
boost::asio::io_context io{};
boost::asio::steady_timer t{io, boost::asio::chrono::seconds{1}};
int count{0};

t.async_wait(boost::bind(
print,
boost::asio::placeholders::error,
std::ref(t),
std::ref(count)));

io.run();

std::cout << "Final count is " << count << std::endl;

return 0;
}
44 changes: 44 additions & 0 deletions src/Timer/4.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <functional>
#include <iostream>

#include <boost/asio.hpp>

namespace {
class Printer {
public:
explicit Printer(boost::asio::io_context& io) : timer{io, boost::asio::chrono::seconds{1}} {
this->timer.async_wait(std::bind(&Printer::print, this));
}

~Printer() {
std::cout << "Final count is " << this->count << std::endl;
}

auto print() -> void {
if (this->count >= 5) {
return;
}

std::cout << this->count << std::endl;

++this->count;

this->timer.expires_at(this->timer.expiry() + boost::asio::chrono::seconds{1});

this->timer.async_wait(std::bind(&Printer::print, this));
}

private:
boost::asio::steady_timer timer;
int count{0};
};
}

auto main() -> int {
boost::asio::io_context io{};
Printer p{io};

io.run();

return 0;
}
64 changes: 64 additions & 0 deletions src/Timer/5.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <functional>
#include <iostream>
#include <thread>

#include <boost/asio.hpp>

namespace {
class Printer {
public:
explicit Printer(boost::asio::io_context& io)
: strand{boost::asio::make_strand(io)},
timer1{io, boost::asio::chrono::seconds{1}},
timer2{io, boost::asio::chrono::seconds{1}} {
this->timer1.async_wait(boost::asio::bind_executor(this->strand, std::bind(&Printer::print1, this)));
this->timer2.async_wait(boost::asio::bind_executor(this->strand, std::bind(&Printer::print2, this)));
}

~Printer() {
std::cout << "Final count is " << this->count << std::endl;
}

auto print1() -> void {
if (this->count >= 10) {
return;
}

std::cout << "Timer 1: " << this->count << std::endl;

++this->count;

this->timer1.expires_at(this->timer1.expiry() + boost::asio::chrono::seconds{1});

this->timer1.async_wait(boost::asio::bind_executor(this->strand, std::bind(&Printer::print1, this)));
}

auto print2() -> void {
if (this->count >= 10) {
return;
}

std::cout << "Timer 2: " << this->count << std::endl;

++this->count;

this->timer2.expires_at(this->timer2.expiry() + boost::asio::chrono::seconds{1});

this->timer2.async_wait(boost::asio::bind_executor(this->strand, std::bind(&Printer::print2, this)));
}

private:
boost::asio::strand<boost::asio::io_context::executor_type> strand;
boost::asio::steady_timer timer1, timer2;
int count{0};
};
}

auto main() -> int {
boost::asio::io_context io{};
Printer p{io};

std::jthread t{[&io] { io.run(); }};

return 0;
}
80 changes: 0 additions & 80 deletions src/main.cc

This file was deleted.

Loading

0 comments on commit 508ea6f

Please sign in to comment.