#ifndef TASKING_H#define TASKING_H#include <QList>#include <QWaitCondition>#include <QMutex>#include <QString>struct task{ int tasknum; QString data;};class Tasking{private: QList<task> *taskList; QWaitCondition *cond; QMutex *mutex;public: Tasking(); ~Tasking(); task getTask(); void addTask(task newTask);};#endif // TASKING_H
#include "Tasking.h"Tasking::Tasking(){ mutex = new QMutex; taskList = new QList<task>; cond = new QWaitCondition;}Tasking::~Tasking(){}task Tasking::getTask(){ if(!(taskList->count()==0)) { mutex->lock(); cond->wait(mutex, ULONG_MAX); mutex->unlock(); } return taskList->last();}void Tasking::addTask(task newTask){ cond->wakeOne(); taskList->push_back(newTask);}
#include "thread1.h"Thread1::Thread1(Tasking *allTasks[], int tasksCount){ Tasks = new Tasking * [tasksCount]; for(int i = 0; i < tasksCount; i++) Tasks[i] = allTasks[i];}void Thread1::run(){ task myTask; myTask.tasknum = 1; myTask.data = "something"; Tasks[0]->addTask(myTask); emit showData(myTask.data);}
#include "thread2.h"Thread2::Thread2(Tasking *allTasks[], int tasksCount){ Tasks = new Tasking * [tasksCount]; for(int i = 0; i < tasksCount; i++) Tasks[i] = allTasks[i];}void Thread2::run(){ task myTask; myTask = Tasks[0]->getTask(); emit showData(myTask.data);}
#include <QtGui/QApplication>#include "mainwidget.h"#include "Tasking.h"#include "thread1.h"#include "thread2.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWidget w; w.show(); Tasking *allTasks[1]; Tasking *tasking1 = new Tasking; allTasks[0] = tasking1; Thread1 myThread1(allTasks, sizeof(allTasks)/sizeof(allTasks[0])); Thread2 myThread2(allTasks, sizeof(allTasks)/sizeof(allTasks[0])); QObject::connect(&myThread1, SIGNAL(showData(QString)), &w, SLOT(addText(QString))); QObject::connect(&myThread2, SIGNAL(showData(QString)), &w, SLOT(addText(QString))); myThread1.start(); myThread2.start(); myThread1.wait(); myThread2.wait(); return a.exec();}
if(!(taskList->count()==0)) { mutex->lock(); cond->wait(mutex, ULONG_MAX); mutex->unlock(); }
C++ (Qt)void Thread2::run( void ){ while (!mStop) { ... }}
void Tasking::addTask(task newTask){ cond->wakeOne(); taskList->push_back(newTask);}
C++ (Qt)class Tasking{private: // Не знаю, для чего использовать здесь указатели, поэтому предлагаю сделать проще: QList<task> taskList; QMutex mutex; QWaitCondition cond; public: task getTask(); void addTask(task newTask);};
C++ (Qt)task Tasking::getTask(){ QMutexLocker locker( &mutex ); while( taskList.empty() ) cond.wait( &mutex ); return taskList.takeFirst();} void Tasking::addTask(task newTask){ QMutexLocker locker( &mutex ); taskList.append( newTask ); cond.wakeOne();}
C++ (Qt)task Tasking::getTask(){ semaphore.acquire(); QMutexLocker locker( &mutex ); return taskList.takeFirst();} void Tasking::addTask(task newTask){ QMutexLocker locker( &mutex ); taskList.append( newTask ); semaphore.release();}
if(taskList.count()==0) { cond.wait(&mutex, ULONG_MAX); }
#include "Tasking.h"Tasking::Tasking(){}Tasking::~Tasking(){}task Tasking::getTask(){ if(taskList.count()==0) { cond.wait(&mutex, ULONG_MAX); } mutex.lock(); task currentTask = taskList.last(); mutex.unlock(); return currentTask;}void Tasking::addTask(task newTask){ mutex.lock(); taskList.push_back(newTask); mutex.unlock(); cond.wakeOne();}
C++ (Qt) QMutexLocker locker( &mutex ); // <<<<<<<<<<< 2 while( taskList.empty() ) // <<<<<<<<<<< 1 cond.wait( &mutex ); return taskList.takeFirst();
task Tasking::getTask(){ mutex.lock(); if(taskList.count()==0) cond.wait(&mutex, ULONG_MAX); task currentTask = taskList.last(); mutex.unlock(); return currentTask;}
C++ (Qt)mutax->unlock(); bool returnValue = d->wait(time);
C++ (Qt) if(taskList.count()==0) cond.wait(&mutex, ULONG_MAX);
C++ (Qt) while( taskList.count()==0 ) cond.wait(&mutex, ULONG_MAX);