sudo apt-get install libmysqlclient-dev libmysqlclient
sudo apt-get install libevent-dev libevent
class CBalanceMySQL { private: // // unsigned int thread_count; // std::vector<MYSQL*> v; // std::vector<event*> events; // std::vector<std::string> queries; struct event_base *evbase; // , // pFunc p_func; // void run(); public: // constructor CBalanceMySQL(); // destructor ~CBalanceMySQL(); // // void setThreadCount(int); // bool ConnectAllThreads(); // bool setSuccessFunc(void(*)(int, short, void*)); };
this->evbase = event_base_new();
bool CBalanceMySQL::ConnectAllThreads() { for (uint i = 0; i < this->thread_count; i++) { MYSQL *m; m = mysql_init(NULL); if (!mysql_real_connect(m, this->host, this->user, this->password, NULL, this->port, NULL, 0)) { std::cout << mysql_error(m); return 0; } else { this->v.push_back(m); } } return 1; }
void CBalanceMySQL::run() { for (uint i = 0; i < this->v.size(); i++) { // struct event *ev; // std::string q = this->queries.at(i); // MYSQL *m = this->v.at(i); // mysql_send_query(m, q.c_str() , strlen(q.c_str())); ev = new event; // // - p_func event_set(ev, m->net.fd, EV_READ, this->p_func, m); event_base_set(this->evbase, ev); event_add(ev, NULL); events.push_back(ev); } // while (0 == event_base_loop(this->evbase, 0)); }
for (uint i = 0; i < v.size(); i++) { mysql_close(this->v.at(i)); } for (uint i = 0; i < events.size(); i++) { delete(this->events.at(i)); } event_base_free(this->evbase);
static void read_result(int fd, short event, void *_userdata) { MYSQL *m = (MYSQL*)_userdata; if (0 == mysql_read_query_result(m)) { MYSQL_RES *res = mysql_store_result(m); MYSQL_ROW row = mysql_fetch_row(res); // SELECT COUNT(*) FROM information_schema.processlist, cout << "cnt for net.fd = " << fd << " is " << row[0] << endl; mysql_free_result(res); } }
Source: https://habr.com/ru/post/144918/
All Articles