generated from rdong8/cpp_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
202 additions
and
252 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.