Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QSqltablemodel make my app crash in SMP

    General and Desktop
    2
    4
    1613
    Loading More Posts
    • 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.
    • C
      cq441745656 last edited by

      i have new a Qsqltablemodel object in my codel .
      but the no-main thread will allways crash my program when my main thread is using the model.
      the code is like this:
      @
      QSqlTableModel* model = new QSqlTableModel;
      void* test(void * p)
      {
      for(int i = 0; i < 10000; i++)
      {
      model->setTable("T_table");
      model->setData(/ignore here/);//mean use model to do some read or write;
      }

      }
      int main()
      {
      QApplication app(argc, argv);

      pthread_create(&id1,NULL,test,0);
      for(int i = 0; i < 10000; i++)
      {
          model->setTable("T_table");
          model->setData(/*ignore here*/);//mean use model to do some read or write;
      }
      return app.exec&#40;&#41;;
      

      }
      @
      I thought it may work well if i lock the read/write lines.

      @

      QMutex lock;
      QSqlTableModel* model = new QSqlTableModel;

      void func()
      {
      lock.lock();
      model->setTable("T_table");
      model->select();
      model->setData(/ignore here/);//means use model to do some read or write;
      .....
      lock.unlock();
      }
      void* test(void * p)
      {
      for(int i = 0; i < 10000; i++)
      {
      func();
      }

      }
      int main()
      {
      QApplication app(argc, argv);

      pthread_create(&id1,NULL,test,0);
      for(int i = 0; i < 10000; i++)
      {
          func();
      }
      return app.exec&#40;&#41;;
      

      }
      @
      but i'am wrong, it crashed again.
      what's reason to make it crash?
      sorry for my poor English.
      hope you understand the meaning and help me out of the trouble .
      Any help is appreciated

      1 Reply Last reply Reply Quote 0
      • V
        volnei last edited by

        Hi,

        take a look at the link below. maybe what is looking.
        But if you encapsulate all access to the database in a single thread
        this might work

        for example:
        http://qt-project.org/doc/qt-4.8/threads-modules.html#threads-and-the-sql-module

        Threads and the SQL Module

        A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.

        In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information

        1 Reply Last reply Reply Quote 0
        • C
          cq441745656 last edited by

          thx,that is really what i am looking for.
          You are a great help--
          because of bad language skill about English,it almost impossible for me to find technical documentation directly.

          1 Reply Last reply Reply Quote 0
          • V
            volnei last edited by

            Hi,
            That I thank you for giving me this opportunity to help you

            By the way, what is your native language?

            1 Reply Last reply Reply Quote 0
            • First post
              Last post