Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. German
  4. QThread mit QMutex Console QMutex:destroying
Forum Updated to NodeBB v4.3 + New Features

QThread mit QMutex Console QMutex:destroying

Scheduled Pinned Locked Moved German
8 Posts 2 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nickvan86
    wrote on last edited by
    #1

    Hallo,

    versuche mich gerade mit Beispielen und Youtube in das Thema QThread einzuarbeiten bevor ich in Richtung Netzwerkprogrammierung gehe.
    Hier mein Code:

    mythread.h

    @#ifndef MYTHREAD_H
    #define MYTHREAD_H
    #include <QtCore>

    class MyThread : public QThread
    {
    public:
    MyThread();
    void run();
    QString name;
    bool Stop;
    };

    #endif // MYTHREAD_H@

    mythread.cpp

    @#include "mythread.h"
    #include <QtCore>
    #include <QtDebug>

    MyThread::MyThread()
    {
    }

    void MyThread::run()
    {
    qDebug() << this->name << " Running";

    for(int i = 0; i<1000;i++)
    {
        QMutex mutex;
        mutex.lock();
        if(this->Stop)
            break;
        mutex.unlock();
        this->sleep(1);
        qDebug() << this->name << i;
    
    }
    

    }@

    Main:

    @#include <QCoreApplication>
    #include <mythread.h>

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    MyThread mThread1;
    mThread1.name = "mThread1";
    MyThread mThread2;
    mThread2.name = "mThread2";
    MyThread mThread3;
    mThread3.name = "mThread3";
    
    mThread1.start();
    //mThread2.start();
    //mThread3.start();
    
    //mThread1.Stop = true;
    
    return a.exec&#40;&#41;;
    

    }@

    Als Ausgabe auf der Konsole bekomme ich folgende Meldung:

    "mThread1" Running
    QMutex: destroying locked mutex

    Ist nicht die Ausgabe, die ich erwartet hätte.

    der @mThread1.Stop = true;@ wurde in der main auskommentiert und so müsste @mThread1.start();@ normal ausgeführt werden ohne in der if (siehe mythread.cpp) die Abbruch Bedingung zu erfüllen. Es müsste bis 999 gezählt werden oder sehe ich hier was falsch?

    Gruß

    Alex

    Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

    1 Reply Last reply
    0
    • T Offline
      T Offline
      topse
      wrote on last edited by
      #2

      Deine Frage enthält leider Fehler und ist unverständlich... vielleicht kannst Du die mal korrigieren?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nickvan86
        wrote on last edited by
        #3

        Hallo,

        sorry hab nicht die cpp kopiert sondern die .h nochmals :-D
        Hab es ausgebessert.

        Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nickvan86
          wrote on last edited by
          #4

          Okay habe jetzt dran rumgespielt.

          Wenn ich folgendes mache:

          @mThread1.Stop = false;
          mThread1.start();@

          Läuft es ganz normal an ohne ein Destroy

          Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

          1 Reply Last reply
          0
          • T Offline
            T Offline
            topse
            wrote on last edited by
            #5

            Du hast glaube ich einen Denkfehler.

            if(this->Stop)
            break;

            sorgt dafür, dass Du die For-Schleife sofort verläßt, also bevor der Mutex "geunlockt" wird. Da der Mutex aber (sinnloserweise) in der Schleife deklariert worden ist, ist die Fehlermeldung vollkommen richtig, dass ein gelockter Mutex destroyed worden ist.

            Der Mutex ist an der Stelle übrigens tatsächlich völlig sinnlos. Wenn Du die Stop-Variable über einen Mutex schützen willst, was übrigens auch überflüssig ist, solltext Du den Mutex als Member von MyThread deklarieren.

            Gruß,
            Tobias

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nickvan86
              wrote on last edited by
              #6

              Also das Beispiel habe ich von folgendem Video heraus:

              http://www.youtube.com/watch?v=5WEiQ3VJfxc&index=31&list=PL2D1942A4688E9D63

              Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

              1 Reply Last reply
              0
              • T Offline
                T Offline
                topse
                wrote on last edited by
                #7

                Mmh... anscheinend hast Du ein gutes Beispiel gefunden, dass man dem Internet nicht alles glauben darf :-/

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nickvan86
                  wrote on last edited by
                  #8

                  Ohje :-(

                  Qt 5.2.1 Creator 3.0.1 Windows 7 64bit

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved