19
19
#include < stdint.h>
20
20
21
21
#include < QDebug>
22
+ #include < QThread>
22
23
#include < QTimer>
23
24
24
25
static int64_t nLastHeaderTipUpdateNotification = 0 ;
@@ -30,22 +31,36 @@ ClientModel::ClientModel(interfaces::Node& node, OptionsModel *_optionsModel, QO
30
31
optionsModel(_optionsModel),
31
32
peerTableModel(nullptr ),
32
33
banTableModel(nullptr ),
33
- pollTimer( nullptr )
34
+ m_thread( new QThread( this ) )
34
35
{
35
36
cachedBestHeaderHeight = -1 ;
36
37
cachedBestHeaderTime = -1 ;
37
38
peerTableModel = new PeerTableModel (m_node, this );
38
39
banTableModel = new BanTableModel (m_node, this );
39
- pollTimer = new QTimer (this );
40
- connect (pollTimer, &QTimer::timeout, this , &ClientModel::updateTimer);
41
- pollTimer->start (MODEL_UPDATE_DELAY);
40
+
41
+ QTimer* timer = new QTimer;
42
+ timer->setInterval (MODEL_UPDATE_DELAY);
43
+ connect (timer, &QTimer::timeout, [this ] {
44
+ // no locking required at this point
45
+ // the following calls will acquire the required lock
46
+ Q_EMIT mempoolSizeChanged (m_node.getMempoolSize (), m_node.getMempoolDynamicUsage ());
47
+ Q_EMIT bytesChanged (m_node.getTotalBytesRecv (), m_node.getTotalBytesSent ());
48
+ });
49
+ connect (m_thread, &QThread::finished, timer, &QObject::deleteLater);
50
+ connect (m_thread, &QThread::started, [timer] { timer->start (); });
51
+ // move timer to thread so that polling doesn't disturb main event loop
52
+ timer->moveToThread (m_thread);
53
+ m_thread->start ();
42
54
43
55
subscribeToCoreSignals ();
44
56
}
45
57
46
58
ClientModel::~ClientModel ()
47
59
{
48
60
unsubscribeFromCoreSignals ();
61
+
62
+ m_thread->quit ();
63
+ m_thread->wait ();
49
64
}
50
65
51
66
int ClientModel::getNumConnections (unsigned int flags) const
@@ -90,14 +105,6 @@ int64_t ClientModel::getHeaderTipTime() const
90
105
return cachedBestHeaderTime;
91
106
}
92
107
93
- void ClientModel::updateTimer ()
94
- {
95
- // no locking required at this point
96
- // the following calls will acquire the required lock
97
- Q_EMIT mempoolSizeChanged (m_node.getMempoolSize (), m_node.getMempoolDynamicUsage ());
98
- Q_EMIT bytesChanged (m_node.getTotalBytesRecv (), m_node.getTotalBytesSent ());
99
- }
100
-
101
108
void ClientModel::updateNumConnections (int numConnections)
102
109
{
103
110
Q_EMIT numConnectionsChanged (numConnections);
0 commit comments