1 class ItemsOpsBase 2 { 3 public: 4 virtual void doOperation(ConnectionView *view) = 0; 5 virtual ~ItemsOpsBase() = default; 6 }; 7 8 class DeleteItem : public ItemsOpsBase 9 { 10 public: 11 DeleteItem(qintptr target,qint32 connectionIndex) 12 :ItemsOpsBase(), _target(target),_connectionIndex(connectionIndex){ } 13 14 void doOperation(ConnectionView *view)override; 15 ~DeleteItem() = default; 16 private: 17 qintptr _target; 18 qint32 _connectionIndex; 19 }; 20 21 class UpdatePulse :public ItemsOpsBase 22 { 23 public: 24 UpdatePulse(qintptr descriptor,qint32 currentTime) 25 :ItemsOpsBase(), _descriptor(descriptor),_currentTime(currentTime){ } 26 27 void doOperation(ConnectionView *view)override; 28 ~UpdatePulse() = default; 29 private: 30 qintptr _descriptor; 31 qint32 _currentTime; 32 }; 33 34 class UpdateRemark : public ItemsOpsBase 35 { 36 public: 37 UpdateRemark(qintptr descriptor, const QString &remark) 38 : ItemsOpsBase(),_remark(remark),_descriptor(descriptor){ } 39 40 void doOperation(ConnectionView *view)override; 41 ~UpdateRemark() = default; 42 private: 43 QString _remark; 44 qintptr _descriptor; 45 }; 46 47 class TestConnection : public ItemsOpsBase 48 { 49 public: 50 void doOperation(ConnectionView *view)override; 51 }; 52 class TestConnectionProducer : public QThread 53 { 54 public: 55 void run()override; 56 }; 57 58 class CopySelectedItemInformProducer : public QThread 59 { 60 public: 61 void run()override; 62 }; 63 64 class DisconnectTargetsProducer : public QThread 65 { 66 public: 67 void run()override; 68 }; 69 70 class DeleteItemProducer :public QThread 71 { 72 public: 73 DeleteItemProducer(qintptr target, qint32 connectionIndex) 74 : QThread(),_target(target),_connectionIndex(connectionIndex) { } 75 void run()override; 76 private: 77 qintptr _target; 78 qint32 _connectionIndex; 79 }; 80 81 class UpdatePulseProducer :public QThread 82 { 83 public: 84 UpdatePulseProducer(qintptr descriptor, qint32 currentTime) 85 :QThread(),_descriptor(descriptor),_currentTime(currentTime){ } 86 protected: 87 void run()override; 88 private: 89 qintptr _descriptor; 90 qint32 _currentTime; 91 }; 92 93 class UpdateRemarkProducer : public QThread 94 { 95 public: 96 UpdateRemarkProducer(qintptr descriptor, const QString &remark) 97 :QThread(),_remark(remark),_descriptor(descriptor){ } 98 protected: 99 void run()override; 100 private: 101 QString _remark; 102 qintptr _descriptor; 103 }; 104 class ConsumerHelper :public QThread 105 { 106 public: 107 ConsumerHelper(ConnectionView *view) 108 :QThread(),_view(view){ } 109 ~ConsumerHelper(); 110 protected: 111 void run() override; 112 private: 113 ConnectionView *_view; 114 115 ConsumerHelper(const ConsumerHelper &other) = delete; 116 ConsumerHelper(const ConsumerHelper &&other) = delete; 117 ConsumerHelper &operator=(const ConsumerHelper &other) = delete; 118 };
1 static QQueue<QSharedPointer<ItemsOpsBase>> &opQueue() 2 { 3 static QQueue<QSharedPointer<ItemsOpsBase>> queue; 4 return queue; 5 } 6 7 static QSharedPointer<ItemsOpsBase> endOperation; 8 9 static QMutex &opQueueLock() 10 { 11 static QMutex mutex; 12 return mutex; 13 } 14 static QWaitCondition &opQueueIsAvailable() 15 { 16 static QWaitCondition flag; 17 return flag; 18 }
1 void DeleteItem::doOperation(ConnectionView *view) 2 { 3 qRegisterMetaType<qintptr>("qintptr"); 4 qRegisterMetaType<TcpConnectionHandler *>("TcpConnectionHandler *"); 5 QMetaObject::invokeMethod(view, "deleteConnection",Qt::QueuedConnection, Q_ARG(qintptr, _target), Q_ARG(qint32, _connectionIndex)); 6 } 7 void DeleteItemProducer::run() 8 { 9 QSharedPointer<ItemsOpsBase> op = QSharedPointer<ItemsOpsBase>(new DeleteItem(_target,_connectionIndex)); 10 11 QMutexLocker locker(&opQueueLock()); 12 opQueue().enqueue(op); 13 opQueueIsAvailable().wakeOne(); 14 }
1 void ConsumerHelper::run() 2 { 3 forever 4 { 5 QSharedPointer<ItemsOpsBase> opPointer; 6 7 { 8 QMutexLocker locker(&opQueueLock()); 9 10 if (opQueue().isEmpty()) 11 opQueueIsAvailable().wait(&opQueueLock()); 12 opPointer = opQueue().dequeue(); 13 14 if (opPointer == endOperation) 15 break; 16 } 17 { 18 if(!opPointer.isNull()) 19 opPointer->doOperation(_view); 20 } 21 } 22 } 23 24 ConsumerHelper::~ConsumerHelper() 25 { 26 { 27 QMutexLocker locker(&opQueueLock()); 28 while(!opQueue().isEmpty()) 29 opQueue().dequeue(); 30 31 opQueue().enqueue(endOperation); 32 opQueueIsAvailable().wakeOne(); 33 } 34 35 wait();//注意这里是wait在次线程上的 36 }
DeleteItemProducer *deleteItemProducer = new DeleteItemProducer(target,index); connect(deleteItemProducer, &QThread::finished, deleteItemProducer, &QThread::deleteLater); deleteItemProducer->start();
原文:http://www.cnblogs.com/Philip-Tell-Truth/p/6295186.html