Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
c8f6aa877c | |||
4283f32826 |
@ -27,6 +27,7 @@ add_subdirectory(test)
|
||||
|
||||
add_compile_options(-w)
|
||||
|
||||
# add_definitions(-DASYNCO_THREADS_POOL_SIZE=20)
|
||||
|
||||
|
||||
# Instaliraj biblioteku
|
||||
|
@ -18,12 +18,20 @@ using namespace std;
|
||||
#endif
|
||||
using namespace boost::asio;
|
||||
|
||||
#define loop while(true)
|
||||
|
||||
#include "timers.hpp"
|
||||
#include "trigger.hpp"
|
||||
|
||||
namespace marcelb {
|
||||
namespace asynco {
|
||||
|
||||
|
||||
struct SleepHandle {
|
||||
std::future<void> future;
|
||||
std::shared_ptr<Timer> timer;
|
||||
};
|
||||
|
||||
/**
|
||||
* Asynco runtime
|
||||
* Used for all asynchronous capabilities of this wrapper
|
||||
@ -170,6 +178,15 @@ public:
|
||||
|
||||
Timer periodic(function<void()> callback, uint64_t time);
|
||||
|
||||
/**
|
||||
* Nonblock time sleep function
|
||||
*/
|
||||
|
||||
void sleep(int _time);
|
||||
|
||||
SleepHandle sleep2(int _time);
|
||||
|
||||
|
||||
/**
|
||||
* Initialize trigger (typed event)
|
||||
*/
|
||||
|
@ -97,6 +97,13 @@ Timer delayed(function<void()> callback, uint64_t time);
|
||||
|
||||
Timer periodic(function<void()> callback, uint64_t time);
|
||||
|
||||
/**
|
||||
* Nonblock time sleep function
|
||||
*/
|
||||
|
||||
void sleep(int _time);
|
||||
SleepHandle sleep2(int _time);
|
||||
|
||||
/**
|
||||
* Initialize trigger (typed event)
|
||||
*/
|
||||
|
@ -37,5 +37,23 @@ Timer Asynco::periodic(function<void()> callback, uint64_t time) {
|
||||
return Timer(io_ctx, callback, time, TimerType::Periodic);
|
||||
}
|
||||
|
||||
void Asynco::sleep(int _time) {
|
||||
promise<void> _promise;
|
||||
Timer t = delayed( [&]() {
|
||||
_promise.set_value();
|
||||
}, _time);
|
||||
|
||||
return await(_promise.get_future());
|
||||
}
|
||||
|
||||
SleepHandle Asynco::sleep2(int _time) {
|
||||
auto _promise = std::make_shared<std::promise<void>>();
|
||||
|
||||
auto _timer = std::make_shared<Timer>(io_ctx, [_promise]() {
|
||||
_promise->set_value();
|
||||
}, _time, TimerType::Delayed);
|
||||
|
||||
return { _promise->get_future(), _timer };
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -13,12 +13,25 @@ Timer periodic(function<void()> callback, uint64_t time) {
|
||||
return Timer(Asynco_Default_Runtime.io_ctx, callback, time, TimerType::Periodic);
|
||||
}
|
||||
|
||||
void sleep(int _time) {
|
||||
return Asynco_Default_Runtime.sleep(_time);
|
||||
}
|
||||
|
||||
SleepHandle sleep2(int _time) {
|
||||
return Asynco_Default_Runtime.sleep2(_time);
|
||||
}
|
||||
|
||||
|
||||
Asynco& asynco_default_runtime() {
|
||||
return Asynco_Default_Runtime;
|
||||
}
|
||||
|
||||
void asynco_default_run() {
|
||||
#ifdef ASYNCO_THREADS_POOL_SIZE
|
||||
Asynco_Default_Runtime.run(ASYNCO_THREADS_POOL_SIZE);
|
||||
#else
|
||||
Asynco_Default_Runtime.run();
|
||||
#endif
|
||||
}
|
||||
|
||||
void asynco_default_run_on_this() {
|
||||
|
@ -27,3 +27,9 @@ target_link_libraries(asynco_coroutine_default asynco Boost::system)
|
||||
|
||||
add_executable(asynco_coroutine main_coroutine.cpp)
|
||||
target_link_libraries(asynco_coroutine asynco Boost::system)
|
||||
|
||||
add_executable(asynco_infinit_loop_default main_infinit_loop_default.cpp)
|
||||
target_link_libraries(asynco_infinit_loop_default asynco Boost::system)
|
||||
|
||||
add_executable(asynco_infinit_loop main_infinit_loop.cpp)
|
||||
target_link_libraries(asynco_infinit_loop asynco Boost::system)
|
@ -11,8 +11,15 @@ void notLambdaFunction() {
|
||||
|
||||
class clm {
|
||||
public:
|
||||
int i = 7;
|
||||
void classMethode() {
|
||||
cout << "Call class method" << endl;
|
||||
cout << "Call class method" << i << endl;
|
||||
}
|
||||
|
||||
future<void> asyncMethode() {
|
||||
return async_([&]() {
|
||||
cout << "Async class method" << i << endl;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@ -53,6 +60,8 @@ int main() {
|
||||
classes.classMethode();
|
||||
});
|
||||
|
||||
await_(classes.asyncMethode());
|
||||
|
||||
//------------------AWAIT----------------------
|
||||
|
||||
auto a = async_ ( []() {
|
||||
|
111
test/main_infinit_loop.cpp
Normal file
111
test/main_infinit_loop.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
#include "../lib/asynco.hpp"
|
||||
using namespace marcelb::asynco;
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main() {
|
||||
Asynco asynco;
|
||||
asynco.run(4);
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 1" << endl;
|
||||
// asynco.sleep(1000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 2" << endl;
|
||||
// asynco.sleep(2000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 25" << endl;
|
||||
// asynco.sleep(2500);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 3" << endl;
|
||||
// asynco.sleep(3000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 35" << endl;
|
||||
// asynco.sleep(3500);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 4" << endl;
|
||||
// asynco.sleep(4000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 45" << endl;
|
||||
// asynco.sleep(4500);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 5" << endl;
|
||||
// asynco.sleep(5000);
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 55" << endl;
|
||||
// asynco.sleep(5500);
|
||||
// }
|
||||
// });
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 6" << endl;
|
||||
// asynco.sleep(6000);
|
||||
// }
|
||||
// });
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 65" << endl;
|
||||
// asynco.sleep(6500);
|
||||
// }
|
||||
// });
|
||||
|
||||
// asynco.async ([&](){
|
||||
// loop {
|
||||
// cout << "Loop 7" << endl;
|
||||
// asynco.sleep(7000);
|
||||
// }
|
||||
// });
|
||||
|
||||
// loop { // blokira trenutnu
|
||||
// cout << "Loop 15" << endl;
|
||||
// asynco.sleep(1500);
|
||||
// }
|
||||
|
||||
asynco.join();
|
||||
return 0;
|
||||
}
|
111
test/main_infinit_loop_default.cpp
Normal file
111
test/main_infinit_loop_default.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
#include "../lib/asynco_default.hpp"
|
||||
using namespace marcelb::asynco;
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main() {
|
||||
asynco_default_run();
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 1" << endl;
|
||||
await_(sleep2(1000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 2" << endl;
|
||||
await_(sleep2(2000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 25" << endl;
|
||||
await_(sleep2(2500).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 3" << endl;
|
||||
await_(sleep2(3000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 35" << endl;
|
||||
await_(sleep2(3500).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 4" << endl;
|
||||
await_(sleep2(4000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 45" << endl;
|
||||
await_(sleep2(4500).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 5" << endl;
|
||||
await_(sleep2(5000).future);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 55" << endl;
|
||||
await_(sleep2(5500).future);
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 6" << endl;
|
||||
await_(sleep2(6000).future);
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 65" << endl;
|
||||
await_(sleep2(6500).future);
|
||||
}
|
||||
});
|
||||
|
||||
async_ ([](){
|
||||
loop {
|
||||
cout << "Loop 7" << endl;
|
||||
await_(sleep2(7000).future);
|
||||
}
|
||||
});
|
||||
|
||||
loop { // blokira trenutnu
|
||||
cout << "Loop 15" << endl;
|
||||
sleep2(1500).future.get();
|
||||
}
|
||||
|
||||
asynco_default_join();
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user