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. In my Qt Multi-threaded program, moveToThread() & QCoreApplication::postEvent() dose work only msleep(1) is followed

In my Qt Multi-threaded program, moveToThread() & QCoreApplication::postEvent() dose work only msleep(1) is followed

Scheduled Pinned Locked Moved General and Desktop
posteventmulti-threadsleepmovetothread
2 Posts 2 Posters 1.4k 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.
  • S Offline
    S Offline
    sinjohr
    wrote on last edited by
    #1

    I am writing a 2-threaded qt program, one thread is main(gui) thread and the other is network thread.

    My problem is that the moveToThread() & QCoreApplication::postEvent() mechanism only works when provided with the following msleep() call.

    In my network thread, the following code has a problem,

    ServerWindowWrapper wrapper;
    wrapper.moveToThread(QApplication::instance()->thread());
    QEvent* ev = new QEvent();
    QCoreApplication::postEvent(&wrapper, ev);
    //msleep(1);
    

    In this case, my posted event is ignored. However, if i uncommented the last line "//msleep(1)", the program works fine as I expected.

    Someone told me that an eventloop mechanism depends on sleep dose mean the software design is wrong.

    So, I want to know whether there is a way to remove "msleep()" without changing my program's behaviors.

    T 1 Reply Last reply
    0
    • S sinjohr

      I am writing a 2-threaded qt program, one thread is main(gui) thread and the other is network thread.

      My problem is that the moveToThread() & QCoreApplication::postEvent() mechanism only works when provided with the following msleep() call.

      In my network thread, the following code has a problem,

      ServerWindowWrapper wrapper;
      wrapper.moveToThread(QApplication::instance()->thread());
      QEvent* ev = new QEvent();
      QCoreApplication::postEvent(&wrapper, ev);
      //msleep(1);
      

      In this case, my posted event is ignored. However, if i uncommented the last line "//msleep(1)", the program works fine as I expected.

      Someone told me that an eventloop mechanism depends on sleep dose mean the software design is wrong.

      So, I want to know whether there is a way to remove "msleep()" without changing my program's behaviors.

      T Offline
      T Offline
      tarod.net
      wrote on last edited by
      #2

      @sinjohr I think this things happen. Working with threads is not always a cool path.

      According to the documentation, a QEvent::ThreadChange event is sent to the object just before the thread affinity is changed.

      You can try to handle the event.

      Another option is using the overloaded method

      void QCoreApplication::postEvent(QObject * receiver, QEvent * event, int priority)
      

      and use Qt::LowEventPriority.

      "Individually, we are one drop. Together, we are an ocean."

      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