int main(int argc, char** argv) { NotificationQueue queue; // create some worker threads Worker worker1("Worker 1", queue); Worker worker2("Worker 2", queue); Worker worker3("Worker 3", queue); MyProducer producer1("Producer1", queue); Thread tWorker1, tWorker2, tWorker3, tProducer1; tWorker1.start(worker1); tWorker2.start(worker2); tWorker3.start(worker3); //Thread::sleep(3000); tProducer1.start(producer1); tProducer1.join(); Thread::sleep(20000); // stop all worker threads queue.wakeUpAll(); return 0; }
void QNetworkReplyImplPrivate::handleNotifications() { if (notificationHandlingPaused) return; NotificationQueue current = pendingNotifications; pendingNotifications.clear(); if (state != Working) return; while (state == Working && !current.isEmpty()) { InternalNotifications notification = current.dequeue(); switch (notification) { case NotifyDownstreamReadyWrite: if (copyDevice) _q_copyReadyRead(); else backend->downstreamReadyWrite(); break; case NotifyCloseDownstreamChannel: backend->closeDownstreamChannel(); break; case NotifyCopyFinished: { QIODevice *dev = copyDevice; copyDevice = 0; backend->copyFinished(dev); break; } } } }
int main(int argc, char** argv) { NotificationQueue queue; // create some worker threads Worker worker1("Worker 1", queue); Worker worker2("Worker 2", queue); Worker worker3("Worker 3", queue); // start worker threads ThreadPool::defaultPool().start(worker1); ThreadPool::defaultPool().start(worker2); ThreadPool::defaultPool().start(worker3); // distribute some work for (int i = 0; i < 50; ++i) { queue.enqueueNotification(new WorkNotification(i)); } // wait until queue is empty and all threads are // waiting for new work. while (!queue.empty()) Thread::sleep(200); Thread::sleep(500); // stop all worker threads queue.wakeUpAll(); ThreadPool::defaultPool().joinAll(); return 0; }
void DatabaseTracker::notifyDatabasesChanged() { // Note that if DatabaseTracker ever becomes non-singleton, we'll have to amend this notification // mechanism to include which tracker the notification goes out on as well. DatabaseTracker& theTracker(tracker()); NotificationQueue notifications; { LockHolder locker(notificationMutex()); notifications.swap(notificationQueue()); notificationScheduled = false; } if (!theTracker.m_client) return; for (unsigned i = 0; i < notifications.size(); ++i) theTracker.m_client->dispatchDidModifyDatabase(notifications[i].first.get(), notifications[i].second); }
void NotificationQueueTest::testQueueDequeueUrgent() { NotificationQueue queue; queue.enqueueNotification(new QTestNotification("first")); queue.enqueueNotification(new QTestNotification("second")); queue.enqueueUrgentNotification(new QTestNotification("third")); assert (!queue.empty()); assert (queue.size() == 3); QTestNotification* pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); assertNotNullPtr(pTNf); assert (pTNf->data() == "third"); pTNf->release(); assert (!queue.empty()); assert (queue.size() == 2); pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); assert (pTNf->data() == "first"); pTNf->release(); assert (!queue.empty()); assert (queue.size() == 1); pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); assertNotNullPtr(pTNf); assert (pTNf->data() == "second"); pTNf->release(); assert (queue.empty()); assert (queue.size() == 0); Notification* pNf = queue.dequeueNotification(); assertNullPtr(pNf); }
void NotificationQueueTest::testQueueRemove() { NotificationQueue queue; Notification::Ptr frontNotification = new QTestNotification("front"); Notification::Ptr middleNotification = new QTestNotification("middle"); Notification::Ptr backNotification = new QTestNotification("back"); queue.enqueueNotification(frontNotification); queue.enqueueNotification(new QTestNotification("dummy")); queue.enqueueNotification(middleNotification); queue.enqueueNotification(new QTestNotification("dummy")); queue.enqueueNotification(backNotification); assert (queue.size() == 5); assert (queue.remove(frontNotification)); assert (queue.size() == 4); assert (queue.remove(middleNotification)); assert (queue.size() == 3); assert (queue.remove(backNotification)); assert (queue.size() == 2); assert (!queue.remove(backNotification)); assert (queue.size() == 2); }
void NotificationQueueTest::testWaitDequeue() { NotificationQueue queue; queue.enqueueNotification(new QTestNotification("third")); queue.enqueueNotification(new QTestNotification("fourth")); assert (!queue.empty()); assert (queue.size() == 2); QTestNotification* pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(10)); assertNotNullPtr(pTNf); assert (pTNf->data() == "third"); pTNf->release(); assert (!queue.empty()); assert (queue.size() == 1); pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(10)); assertNotNullPtr(pTNf); assert (pTNf->data() == "fourth"); pTNf->release(); assert (queue.empty()); assert (queue.size() == 0); Notification* pNf = queue.waitDequeueNotification(10); assertNullPtr(pNf); }