Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqltablemodel make my app crash in SMP
Qt 6.12 LTS is out! See what's new in the release blog

QSqltablemodel make my app crash in SMP

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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
          0

          • Login

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