added multi threaded pool test
This commit is contained in:
@@ -86,19 +86,21 @@ public:
|
||||
}
|
||||
|
||||
connection_pointer acquire() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
while (idle_connections_.empty()) {
|
||||
cv.wait(lock);
|
||||
}
|
||||
|
||||
return get_next_connection();
|
||||
}
|
||||
|
||||
connection_pointer try_acquire() {
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
if (idle_connections_.empty()) {
|
||||
return {nullptr, this};
|
||||
}
|
||||
|
||||
pointer next_connection{nullptr};
|
||||
for (auto &item : idle_connections_) {
|
||||
next_connection = item.second;
|
||||
auto node = idle_connections_.extract(item.first);
|
||||
inuse_connections_.insert(std::move(node));
|
||||
break;
|
||||
}
|
||||
return {next_connection, this};
|
||||
return get_next_connection();
|
||||
}
|
||||
|
||||
connection_pointer acquire(size_t id) {
|
||||
@@ -151,8 +153,21 @@ public:
|
||||
const connection_info &info() const {
|
||||
return info_;
|
||||
}
|
||||
|
||||
private:
|
||||
connection_pointer get_next_connection() {
|
||||
pointer next_connection{nullptr};
|
||||
for (auto &item : idle_connections_) {
|
||||
next_connection = item.second;
|
||||
auto node = idle_connections_.extract(item.first);
|
||||
inuse_connections_.insert(std::move(node));
|
||||
break;
|
||||
}
|
||||
return {next_connection, this};
|
||||
}
|
||||
private:
|
||||
mutable std::mutex mutex_;
|
||||
std::condition_variable cv;
|
||||
std::vector<IdConnection<Connection>> connection_repo_;
|
||||
using pointer = IdConnection<Connection>*;
|
||||
using connection_map = std::unordered_map<size_t, pointer>;
|
||||
|
||||
Reference in New Issue
Block a user