update unique ptr.

This commit is contained in:
caozhiyi
2021-06-17 17:00:59 +08:00
parent 20aa93ff38
commit fb84bc0060
12 changed files with 28 additions and 27 deletions

View File

@@ -22,7 +22,7 @@ public:
virtual void Start() {
_stop = false;
if (!_thread) {
_thread = std::make_shared<std::thread>(std::bind(&Thread::Run, this));
_thread = std::unique_ptr<std::thread>(new std::thread(std::bind(&Thread::Run, this)));
}
}
@@ -48,7 +48,7 @@ protected:
protected:
std::atomic_bool _stop;
std::shared_ptr<std::thread> _thread;
std::unique_ptr<std::thread> _thread;
};
}

View File

@@ -10,23 +10,23 @@
namespace cppnet {
std::shared_ptr<Timer> MakeTimer50Ms() {
return std::make_shared<Timer1ms>();
std::unique_ptr<Timer> MakeTimer50Ms() {
return std::unique_ptr<Timer>(new Timer1ms());
}
std::shared_ptr<Timer> MakeTimer1Sec() {
std::unique_ptr<Timer> MakeTimer1Sec() {
auto sub = MakeTimer50Ms();
return std::make_shared<TimerContainer>(sub, TC_50MS, TC_1SEC);
return std::unique_ptr<Timer>(new TimerContainer(std::move(sub), TC_50MS, TC_1SEC));
}
std::shared_ptr<Timer> MakeTimer1Min() {
std::unique_ptr<Timer> MakeTimer1Min() {
auto sub = MakeTimer1Sec();
return std::make_shared<TimerContainer>(sub, TC_1SEC, TC_1MIN);
return std::unique_ptr<Timer>(new TimerContainer(std::move(sub), TC_1SEC, TC_1MIN));
}
std::shared_ptr<Timer> MakeTimer1Hour() {
std::unique_ptr<Timer> MakeTimer1Hour() {
auto sub = MakeTimer1Min();
return std::make_shared<TimerContainer>(sub, TC_1MIN, TC_1HOUR);
return std::unique_ptr<Timer>(new TimerContainer(std::move(sub), TC_1MIN, TC_1HOUR));
}
}

View File

@@ -11,13 +11,13 @@
namespace cppnet {
std::shared_ptr<Timer> MakeTimer50Ms();
std::unique_ptr<Timer> MakeTimer50Ms();
std::shared_ptr<Timer> MakeTimer1Sec();
std::unique_ptr<Timer> MakeTimer1Sec();
std::shared_ptr<Timer> MakeTimer1Min();
std::unique_ptr<Timer> MakeTimer1Min();
std::shared_ptr<Timer> MakeTimer1Hour();
std::unique_ptr<Timer> MakeTimer1Hour();
}

View File

@@ -10,12 +10,12 @@
namespace cppnet {
TimerContainer::TimerContainer(std::shared_ptr<Timer> t, TIMER_CAPACITY accuracy, TIMER_CAPACITY capacity) :
_sub_timer(t),
TimerContainer::TimerContainer(std::unique_ptr<Timer> t, TIMER_CAPACITY accuracy, TIMER_CAPACITY capacity) :
_sub_timer(std::move(t)),
_cur_index(0),
_accuracy(accuracy),
_capacity(capacity) {
assert(t);
_max_size = capacity/accuracy;
_timer_wheel.resize(_max_size);
_bitmap.Init(_max_size);

View File

@@ -19,7 +19,7 @@ class TimerContainer:
public Timer {
public:
TimerContainer(std::shared_ptr<Timer> t, TIMER_CAPACITY accuracy, TIMER_CAPACITY capacity);
TimerContainer(std::unique_ptr<Timer> t, TIMER_CAPACITY accuracy, TIMER_CAPACITY capacity);
~TimerContainer();
bool AddTimer(std::weak_ptr<TimerSolt> t, uint32_t time, bool always = false);
@@ -44,7 +44,7 @@ private:
private:
std::vector<std::list<std::weak_ptr<TimerSolt>>> _timer_wheel;
std::shared_ptr<Timer> _sub_timer;
std::unique_ptr<Timer> _sub_timer;
uint32_t _cur_index;
Bitmap _bitmap;

View File

@@ -44,7 +44,7 @@ void CppNetBase::Init(uint32_t thread_num) {
if (thread_num == 0 || thread_num >= cpus * 2) {
thread_num = cpus;
}
_random = std::make_shared<RangeRandom>(0, thread_num - 1);
_random = std::unique_ptr<RangeRandom>(new RangeRandom(0, thread_num - 1));
#ifndef __win__
//Disable SIGPIPE signal

View File

@@ -64,7 +64,7 @@ private:
connect_call_back _disconnect_cb;
connect_call_back _accept_cb;
std::shared_ptr<RangeRandom> _random;
std::unique_ptr<RangeRandom> _random;
std::vector<std::shared_ptr<Dispatcher>> _dispatchers;
};

View File

@@ -63,7 +63,7 @@ private:
std::vector<Task> _task_list;
std::thread::id _local_thread_id;
std::shared_ptr<Timer> _timer;
std::unique_ptr<Timer> _timer;
std::shared_ptr<EventActions> _event_actions;
std::weak_ptr<CppNetBase> _cppnet_base;

View File

@@ -3,8 +3,8 @@
// Author: caozhiyi (caozhiyi5@gmail.com)
#ifndef NET_EVENT_LINUX_EPOLL_ACTION
#define NET_EVENT_LINUX_EPOLL_ACTION
#ifndef NET_EVENT_EPOLL_EPOLL_ACTION
#define NET_EVENT_EPOLL_EPOLL_ACTION
#include <vector>
#ifdef __win__

View File

@@ -3,8 +3,8 @@
// Author: caozhiyi (caozhiyi5@gmail.com)
#ifndef NET_EVENT_MAC_KQUEUE_ACTION
#define NET_EVENT_MAC_KQUEUE_ACTION
#ifndef NET_EVENT_KQUEUE_KQUEUE_ACTION
#define NET_EVENT_KQUEUE_KQUEUE_ACTION
#include <mutex>
#include <vector>

View File

@@ -90,7 +90,7 @@ void ConnectSocket::Accept() {
}
void ConnectSocket::Close() {
// TODO
}
void ConnectSocket::OnAccept() {

View File

@@ -204,6 +204,7 @@ void RWSocket::OnConnect(uint16_t err) {
void RWSocket::OnDisConnect(uint16_t err) {
auto sock = shared_from_this();
__all_socket_map.erase(_sock);
LOG_ERROR("socket left num: %d, TheadId: %ld", __all_socket_map.size(), std::this_thread::get_id());
if (!IsShutdown()) {
auto cppnet_base = _cppnet_base.lock();