Async fork start commit

for_fork
mbandic 5 months ago
parent c84fcf5bf6
commit 33d25fb181
  1. 4
      lib/mysql.hpp
  2. 14
      src/mysql.cpp
  3. 47
      test/test.cpp

@ -154,7 +154,7 @@ class MySQL {
* Internal tloop periodic
*/
void _tloop();
void _tloop(uint32_t b, uint32_t e);
public:
function<void(const string&)> on_error;
@ -219,7 +219,7 @@ public:
* please call this function in it for proper operation at a certain time interval.
* You can use the default MYSQL_PERIODIC_INTERNAL_TIME
*/
void tloop();
void tloop(uint32_t b, uint32_t e);
/**
* Destruktor

@ -15,7 +15,7 @@ marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const s
tloop_future = async(launch::async, [&](){
while (run_tloop) {
usleep(MYSQL_PERIODIC_INTERNAL_TIME*1000);
_tloop();
_tloop(0, connection_pool.size());
}
return;
});
@ -93,11 +93,11 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) {
return status;
}
void marcelb::mysql::MySQL::_tloop() {
void marcelb::mysql::MySQL::_tloop(uint32_t b, uint32_t e) {
if (!run_tloop) {
return;
}
for (size_t i=0; i<connection_pool.size(); i++) {
for (size_t i=b; i<connection_pool.size() && i<e; i++) {
try {
Connection *conn = nullptr;
{
@ -112,13 +112,13 @@ void marcelb::mysql::MySQL::_tloop() {
} else {
cout << "Nije validno----" << endl;
if (!conn->isClosed()){
cout << "Zatvori----" << endl;
cout << "Zatvori----" << endl;
conn->close();
}
Connection *n_conn = create_connection();
{
cout << "Otvori----" << endl;
lock_guard<mutex> lock(io);
connection_pool.push(n_conn);
condition.notify_one();
@ -168,12 +168,12 @@ marcelb::mysql::MySQL::~MySQL() {
}
void marcelb::mysql::MySQL::tloop() {
void marcelb::mysql::MySQL::tloop(uint32_t b, uint32_t e) {
if (tloop_type == time_loop_type::internal) {
if (on_error) {
on_error("Can't start external call tloop, internal is active!");
}
return;
}
_tloop();
_tloop(b,e);
}

@ -14,8 +14,9 @@ using namespace marcelb::asynco;
int main() {
auto inis = rtime_ms();
try {
const int n = 30;
// MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal);
MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 12, time_loop_type::external);
MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", n, time_loop_type::external);
// MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
cout << "init: " << rtime_ms() - inis << endl;
@ -25,14 +26,44 @@ int main() {
};
periodic mysql_tloop ( [&mydb] () {
try {
auto start = rtime_ms();
mydb.tloop();
cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl;
} catch (...) {
cout << "Bude neki error u loopu" << endl;
auto l_start = rtime_ms();
vector<future<void>> to_wait;
for (int i=0, old_i=0; i<n; old_i=i) {
i += 5;
to_wait.push_back( nonsync ([&, i, old_i](){
try {
auto start = rtime_ms();
mydb.tloop(old_i, i);
cout << "old " << old_i << " i " << i << endl;
cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl;
} catch (...) {
cout << "Bude neki error u loopu" << endl;
}
}));
}
// nonsync ([&](){
// try {
// auto start = rtime_ms();
// mydb.tloop(4, 8);
// cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl;
// } catch (...) {
// cout << "Bude neki error u loopu" << endl;
// }
// });
// nonsync ([&](){
// try {
// auto start = rtime_ms();
// mydb.tloop(8, 12);
// cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl;
// } catch (...) {
// cout << "Bude neki error u loopu" << endl;
// }
// });
for (auto& tw : to_wait) {
wait (tw);
}
}, 2000);
cout << "all loop !!!!!!!!!!!!!!1, trajalo: " << rtime_ms() - l_start << endl;
}, 5000);
while (true) {
sleep(60);

Loading…
Cancel
Save