Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multi-process or multi-threaded start QApplication, The GUI does not work properly

Multi-process or multi-threaded start QApplication, The GUI does not work properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 769 Views
  • 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.
  • W Offline
    W Offline
    wang-stone
    wrote on last edited by
    #1
    • The first scene:
      In main() function,I create a thread using pthread_create(), then start QApplication and main widget in this thread,exit the thread after I close the main widget. When I create a thread and start QApplication and main widget again,I‘m trying to drag a text item in main widget,the main widget will be stuck.
      code:
    void *test(void *)
    {
        QApplication a(m_argc, m_argv);
        MainWindow w;
        w.show();
        a.exec();
        
        return NULL;
    }
    
    int main(int argc, char *argv[])
    {
        Q_UNUSED(argc);
        Q_UNUSED(argv);
    
        while(1) {
            pthread_t gui_thread;
            int err = pthread_create(&gui_thread, NULL, test, NULL);
            if(err) {
                printf("Error: failed to create thread, exit\n");
                exit(1);
            }
            
            pthread_join(gui_thread, NULL);
        }
    
        return 0;
    }
    

    I am debugging it with Qt source code, the MouseRelease event cannot be captured,so the event loop cannot be exited.

    • The second scene:
      In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.
    JonBJ jeremy_kJ 2 Replies Last reply
    0
    • W wang-stone
      • The first scene:
        In main() function,I create a thread using pthread_create(), then start QApplication and main widget in this thread,exit the thread after I close the main widget. When I create a thread and start QApplication and main widget again,I‘m trying to drag a text item in main widget,the main widget will be stuck.
        code:
      void *test(void *)
      {
          QApplication a(m_argc, m_argv);
          MainWindow w;
          w.show();
          a.exec();
          
          return NULL;
      }
      
      int main(int argc, char *argv[])
      {
          Q_UNUSED(argc);
          Q_UNUSED(argv);
      
          while(1) {
              pthread_t gui_thread;
              int err = pthread_create(&gui_thread, NULL, test, NULL);
              if(err) {
                  printf("Error: failed to create thread, exit\n");
                  exit(1);
              }
              
              pthread_join(gui_thread, NULL);
          }
      
          return 0;
      }
      

      I am debugging it with Qt source code, the MouseRelease event cannot be captured,so the event loop cannot be exited.

      • The second scene:
        In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @wang-stone
      I don't know why you are trying to do things this way. For a start I don't think you can re-create a QApplication instance more than once, I don't think Qt infrastructure is designed for this. Nor do I know what state it is in if you create QApplication in a fork()ed sub-process (no exec...()) where it's still sharing data with the caller.

      The normal way of doing things is to allow the Qt event loop to run on the main thread, and do anything else computational in secondary threads if desired.

      You might also consider using Qt's QThread etc. support rather than worrying about any compatibility issues with pthreads.

      W 1 Reply Last reply
      0
      • JonBJ JonB

        @wang-stone
        I don't know why you are trying to do things this way. For a start I don't think you can re-create a QApplication instance more than once, I don't think Qt infrastructure is designed for this. Nor do I know what state it is in if you create QApplication in a fork()ed sub-process (no exec...()) where it's still sharing data with the caller.

        The normal way of doing things is to allow the Qt event loop to run on the main thread, and do anything else computational in secondary threads if desired.

        You might also consider using Qt's QThread etc. support rather than worrying about any compatibility issues with pthreads.

        W Offline
        W Offline
        wang-stone
        wrote on last edited by
        #3

        @JonB Thanks for your answer, It really doesn't work that way.

        1 Reply Last reply
        0
        • W wang-stone
          • The first scene:
            In main() function,I create a thread using pthread_create(), then start QApplication and main widget in this thread,exit the thread after I close the main widget. When I create a thread and start QApplication and main widget again,I‘m trying to drag a text item in main widget,the main widget will be stuck.
            code:
          void *test(void *)
          {
              QApplication a(m_argc, m_argv);
              MainWindow w;
              w.show();
              a.exec();
              
              return NULL;
          }
          
          int main(int argc, char *argv[])
          {
              Q_UNUSED(argc);
              Q_UNUSED(argv);
          
              while(1) {
                  pthread_t gui_thread;
                  int err = pthread_create(&gui_thread, NULL, test, NULL);
                  if(err) {
                      printf("Error: failed to create thread, exit\n");
                      exit(1);
                  }
                  
                  pthread_join(gui_thread, NULL);
              }
          
              return 0;
          }
          

          I am debugging it with Qt source code, the MouseRelease event cannot be captured,so the event loop cannot be exited.

          • The second scene:
            In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.
          jeremy_kJ Offline
          jeremy_kJ Offline
          jeremy_k
          wrote on last edited by
          #4

          @wang-stone said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

          • The second scene:
            In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.

          I don't understand the question. Code will almost certainly make it clearer.

          @JonB said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

          @wang-stone
          I don't know why you are trying to do things this way. For a start I don't think you can re-create a QApplication instance more than once, I don't think Qt infrastructure is designed for this.

          There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.

          The normal way of doing things is to allow the Qt event loop to run on the main thread, and do anything else computational in secondary threads if desired.

          The QApplication can be created in a thread that wasn't created to execute main(), except for macOS, and possibly other platforms. Some global objects are created in the same thread as the QApplication. Trying to create a new QApplication instance in another thread may run into problems attempting to access these objects.

          Asking a question about code? http://eel.is/iso-c++/testcase/

          JonBJ 1 Reply Last reply
          0
          • jeremy_kJ jeremy_k

            @wang-stone said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

            • The second scene:
              In a non-Qt process,I use fork() to create a new process, in subprocess,I start a QApplication and main widget,It's working normally.If I use pthread_create() create a new thread in the main process,and start QApplication in sub thread. I use fork() to create a sub process after the sub-thread exit, then start QApplication and main widget in sub process, the main widget cannot be showed.

            I don't understand the question. Code will almost certainly make it clearer.

            @JonB said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

            @wang-stone
            I don't know why you are trying to do things this way. For a start I don't think you can re-create a QApplication instance more than once, I don't think Qt infrastructure is designed for this.

            There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.

            The normal way of doing things is to allow the Qt event loop to run on the main thread, and do anything else computational in secondary threads if desired.

            The QApplication can be created in a thread that wasn't created to execute main(), except for macOS, and possibly other platforms. Some global objects are created in the same thread as the QApplication. Trying to create a new QApplication instance in another thread may run into problems attempting to access these objects.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @jeremy_k said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

            There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.

            I always respect your expertise. However I would suggest it may not be as straightforward as you imply to create, fully destroy, clean up and then recreate. See for example https://forum.qt.io/topic/153620/please-destroy-the-qapplication-singleton-before-creating-a-new-qapplication-instance. Maybe that is only a PySide/Python thing, I don't know.

            jeremy_kJ 1 Reply Last reply
            0
            • JonBJ JonB

              @jeremy_k said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

              There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.

              I always respect your expertise. However I would suggest it may not be as straightforward as you imply to create, fully destroy, clean up and then recreate. See for example https://forum.qt.io/topic/153620/please-destroy-the-qapplication-singleton-before-creating-a-new-qapplication-instance. Maybe that is only a PySide/Python thing, I don't know.

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on last edited by
              #6

              @JonB said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

              @jeremy_k said in Multi-process or multi-threaded start QApplication, The GUI does not work properly:

              There can only be one Q*Application at a time (otherwise, what would QCoreApplication::instance() return?), but it can be repeatedly created and destroyed.

              I always respect your expertise.

              Ouch! Am I that old? ;-)
              That emoticon may confirm it.

              However I would suggest it may not be as straightforward as you imply to create, fully destroy, clean up and then recreate. See for example https://forum.qt.io/topic/153620/please-destroy-the-qapplication-singleton-before-creating-a-new-qapplication-instance. Maybe that is only a PySide/Python thing, I don't know.

              I saw that, and see 2 3 potential issues.

              • I don't know if python del means that the object is destroyed before the next "sequence point", or is merely unavailable to further python code. I've run into problems in my own code with large memory leaks when the garbage collector had not been executed recently.
              • There's the global objects caveat. This example appears to be in the application programmer's code, but I believe that there has been an instance or two in Qt library code.
              • macOS, but then it wouldn't even appear to work for a single round.

              Asking a question about code? http://eel.is/iso-c++/testcase/

              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