#include "Tasking.h"Tasking::Tasking(){}Tasking::~Tasking(){}task Tasking::getTask(){ while(taskList.empty()) cond.wait(&mutex, ULONG_MAX); mutex.lock(); task currentTask = taskList.takeFirst(); mutex.unlock(); return currentTask;}void Tasking::addTask(task newTask){ mutex.lock(); taskList.append(newTask); mutex.unlock(); cond.wakeOne();}
Starte D:\C++\Tasking-build-desktop\debug\Tasking.exe...ASSERT failure in QMutex::unlock(): "A mutex must be unlocked in the same thread that locked it.", file thread\qmutex.cpp, line 371Invalid parameter passed to C runtime function.Invalid parameter passed to C runtime function.D:\C++\Tasking-build-desktop\debug\Tasking.exe beendet, Rückgabewert 3
task Tasking::getTask(){ QMutexLocker locker(&mutex); while(taskList.empty()) cond.wait(&mutex, ULONG_MAX); return taskList.takeFirst();}void Tasking::addTask(task newTask){ QMutexLocker locker(&mutex); taskList.append(newTask); cond.wakeOne();}
#include <QList>#include "mainwidget.h"#include "Tasking.h"#include "guithread.h"#include "thread1.h"#include "thread2.h"int main(int argc, char *argv[]){ QList<Tasking*> allTasks; Tasking *tasking1 = new Tasking; Tasking *tasking2 = new Tasking; allTasks.append(tasking1); allTasks.append(tasking2); GUIThread guiThread(allTasks, argc, argv); Thread1 myThread1(allTasks); Thread2 myThread2(allTasks); QObject::connect(&myThread1, SIGNAL(showData(QString)), guiThread.w, SLOT(addText(QString))); QObject::connect(&myThread2, SIGNAL(showData(QString)), guiThread.w, SLOT(addText(QString))); guiThread.start(); myThread1.start(); myThread2.start(); guiThread.wait(); myThread1.wait(); myThread2.wait(); return 0;}
#include "guithread.h"GUIThread::GUIThread(const QList<Tasking*>& allTasks, int argc, char *argv[]){ Tasks = allTasks; qapp = new QApplication(argc, argv); w = new MainWidget(Tasks); w->showMW();}void GUIThread::run(){ qapp->exec();}
#include "thread1.h"Thread1::Thread1(const QList<Tasking*>& allTasks){ Tasks = allTasks;}void Thread1::run(){ task myTask; bool exit = FALSE; while(!exit) { myTask = Tasks[0]->getTask(); switch (myTask.tasknum) { case 0: exit = TRUE; case 1: emit showData(myTask.data); Tasks[1]->addTask(myTask); } }}
myThread1.start(); myThread2.start(); qapp.exec(); myThread1.wait(); myThread2.wait();