提交 b5930fe6 编写于 作者: Y youngwolf

Some small optimization.

上级 6de2475b
......@@ -71,8 +71,8 @@ public:
void begin(float max_delay, size_t msg_len)
{
do_something_to_all([msg_len](object_ctype& item) {item->begin(msg_len);});
set_timer(TIMER_END, 5000, [max_delay, this](tid id)->bool {this->do_something_to_all([max_delay](object_ctype& item) {item->check_delay(max_delay);}); return true;});
do_something_to_all([&msg_len](object_ctype& item) {item->begin(msg_len);});
set_timer(TIMER_END, 5000, [max_delay, this](tid id)->bool {this->do_something_to_all([&max_delay](object_ctype& item) {item->check_delay(max_delay);}); return true;});
}
};
......
......@@ -197,7 +197,7 @@ public:
}
void clear_status() {do_something_to_all([](object_ctype& item) {item->clear_status();});}
void begin(size_t msg_num, size_t msg_len, char msg_fill) {do_something_to_all([=](object_ctype& item) {item->begin(msg_num, msg_len, msg_fill);});}
void begin(size_t msg_num, size_t msg_len, char msg_fill) {do_something_to_all([&](object_ctype& item) {item->begin(msg_num, msg_len, msg_fill);});}
void shutdown_some_client(size_t n)
{
......@@ -335,14 +335,14 @@ void send_msg_concurrently(echo_client& client, size_t send_thread_num, size_t m
cpu_timer begin_time;
std::list<std::thread> threads;
do_something_to_all(link_groups, [&threads, msg_num, msg_len, msg_fill](const std::list<echo_client::object_type>& item) {
do_something_to_all(link_groups, [&threads, &msg_num, &msg_len, &msg_fill](const std::list<echo_client::object_type>& item) {
threads.emplace_back([&item, msg_num, msg_len, msg_fill]() {
auto buff = new char[msg_len];
memset(buff, msg_fill, msg_len);
for (size_t i = 0; i < msg_num; ++i)
{
memcpy(buff, &i, sizeof(size_t)); //seq
do_something_to_all(item, [buff, msg_len](echo_client::object_ctype& item2) {item2->safe_send_msg(buff, msg_len, false);}); //can_overflow is false, it's important
do_something_to_all(item, [&buff, &msg_len](echo_client::object_ctype& item2) {item2->safe_send_msg(buff, msg_len, false);}); //can_overflow is false, it's important
}
delete[] buff;
});
......@@ -430,7 +430,7 @@ int main(int argc, const char* argv[])
//method #2, add clients first without any arguments, then set the server address.
for (size_t i = 1; i < link_num / 2; ++i)
client.add_socket();
client.do_something_to_all([port, &ip](echo_client::object_ctype& item) {item->set_server_addr(port, ip);});
client.do_something_to_all([&port, &ip](echo_client::object_ctype& item) {item->set_server_addr(port, ip);});
//method #3, add clients and set server address in one invocation.
for (auto i = std::max((size_t) 1, link_num / 2); i < link_num; ++i)
......
......@@ -109,7 +109,7 @@ class echo_client : public multi_client_base<echo_socket>
public:
echo_client(service_pump& service_pump_) : multi_client_base<echo_socket>(service_pump_) {}
void begin(size_t msg_num, const char* msg, size_t msg_len) {do_something_to_all([=](object_ctype& item) {item->begin(msg_num, msg, msg_len);});}
void begin(size_t msg_num, const char* msg, size_t msg_len) {do_something_to_all([&](object_ctype& item) {item->begin(msg_num, msg, msg_len);});}
};
int main(int argc, const char* argv[])
......
......@@ -657,7 +657,7 @@ TCP_SEND_MSG_CALL_SWITCH(FUNNAME, bool)
#define TCP_BROADCAST_MSG(FUNNAME, SEND_FUNNAME) \
void FUNNAME(const char* const pstr[], const size_t len[], size_t num, bool can_overflow = false, bool prior = false) \
{this->do_something_to_all([=](typename Pool::object_ctype& item) {item->SEND_FUNNAME(pstr, len, num, can_overflow, prior);});} \
{this->do_something_to_all([&](typename Pool::object_ctype& item) {item->SEND_FUNNAME(pstr, len, num, can_overflow, prior);});} \
TCP_SEND_MSG_CALL_SWITCH(FUNNAME, void)
//TCP msg sending interface
///////////////////////////////////////////////////
......
......@@ -195,7 +195,7 @@ public:
object_type invalid_object_find(uint_fast64_t id)
{
std::lock_guard<std::mutex> lock(invalid_object_can_mutex);
auto iter = std::find_if(std::begin(invalid_object_can), std::end(invalid_object_can), [id](object_ctype& item) {return item->is_equal_to(id);});
auto iter = std::find_if(std::begin(invalid_object_can), std::end(invalid_object_can), [&id](object_ctype& item) {return item->is_equal_to(id);});
return iter == std::end(invalid_object_can) ? object_type() : *iter;
}
......@@ -211,7 +211,7 @@ public:
object_type invalid_object_pop(uint_fast64_t id)
{
std::lock_guard<std::mutex> lock(invalid_object_can_mutex);
auto iter = std::find_if(std::begin(invalid_object_can), std::end(invalid_object_can), [id](object_ctype& item) {return item->is_equal_to(id);});
auto iter = std::find_if(std::begin(invalid_object_can), std::end(invalid_object_can), [&id](object_ctype& item) {return item->is_equal_to(id);});
if (iter != std::end(invalid_object_can) && (*iter).unique() && (*iter)->obsoleted())
{
auto object_ptr(std::move(*iter));
......
......@@ -81,7 +81,7 @@ public:
object_type find(int id)
{
std::lock_guard<std::mutex> lock(service_can_mutex);
auto iter = std::find_if(std::begin(service_can), std::end(service_can), [id](object_ctype& item) {return id == item->id();});
auto iter = std::find_if(std::begin(service_can), std::end(service_can), [&id](object_ctype& item) {return id == item->id();});
return iter == std::end(service_can) ? nullptr : *iter;
}
......@@ -99,7 +99,7 @@ public:
void remove(int id)
{
std::unique_lock<std::mutex> lock(service_can_mutex);
auto iter = std::find_if(std::begin(service_can), std::end(service_can), [id](object_ctype& item) {return id == item->id();});
auto iter = std::find_if(std::begin(service_can), std::end(service_can), [&id](object_ctype& item) {return id == item->id();});
if (iter != std::end(service_can))
{
auto i_service_ = *iter;
......
......@@ -77,11 +77,11 @@ public:
//functions with a socket_ptr parameter will remove the link from object pool first, then call corresponding function, if you want to reconnect to the server,
//please call socket_ptr's 'disconnect' 'force_shutdown' or 'graceful_shutdown' with true 'reconnect' directly.
void disconnect(typename Pool::object_ctype& socket_ptr) {this->del_object(socket_ptr); socket_ptr->disconnect();}
void disconnect(bool reconnect = false) {this->do_something_to_all([=](typename Pool::object_ctype& item) {item->disconnect(reconnect);});}
void disconnect(bool reconnect = false) {this->do_something_to_all([&](typename Pool::object_ctype& item) {item->disconnect(reconnect);});}
void force_shutdown(typename Pool::object_ctype& socket_ptr) {this->del_object(socket_ptr); socket_ptr->force_shutdown();}
void force_shutdown(bool reconnect = false) {this->do_something_to_all([=](typename Pool::object_ctype& item) {item->force_shutdown(reconnect);});}
void force_shutdown(bool reconnect = false) {this->do_something_to_all([&](typename Pool::object_ctype& item) {item->force_shutdown(reconnect);});}
void graceful_shutdown(typename Pool::object_ctype& socket_ptr, bool sync = true) {this->del_object(socket_ptr); socket_ptr->graceful_shutdown(false, sync);}
void graceful_shutdown(bool reconnect = false, bool sync = true) {this->do_something_to_all([=](typename Pool::object_ctype& item) {item->graceful_shutdown(reconnect, sync);});}
void graceful_shutdown(bool reconnect = false, bool sync = true) {this->do_something_to_all([&](typename Pool::object_ctype& item) {item->graceful_shutdown(reconnect, sync);});}
protected:
virtual void uninit() {this->stop(); force_shutdown();} //if you wanna graceful shutdown, call graceful_shutdown before service_pump::stop_service invocation.
......
......@@ -146,9 +146,9 @@ public:
//functions with a socket_ptr parameter will remove the link from object pool first, then call corresponding function.
void disconnect(typename Pool::object_ctype& socket_ptr) {this->del_object(socket_ptr); socket_ptr->disconnect();}
void disconnect() {this->do_something_to_all([=](typename Pool::object_ctype& item) {item->disconnect();});}
void disconnect() {this->do_something_to_all([&](typename Pool::object_ctype& item) {item->disconnect();});}
void force_shutdown(typename Pool::object_ctype& socket_ptr) {this->del_object(socket_ptr); socket_ptr->force_shutdown();}
void force_shutdown() {this->do_something_to_all([=](typename Pool::object_ctype& item) {item->force_shutdown();});}
void force_shutdown() {this->do_something_to_all([&](typename Pool::object_ctype& item) {item->force_shutdown();});}
void graceful_shutdown(typename Pool::object_ctype& socket_ptr, bool sync = false) {this->del_object(socket_ptr); socket_ptr->graceful_shutdown(sync);}
void graceful_shutdown() {this->do_something_to_all([](typename Pool::object_ctype& item) {item->graceful_shutdown();});} //parameter sync must be false (the default value), or dead lock will occur.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册